Thursday, 5 September 2013

regex allows one character (it should not) why?

regex allows one character (it should not) why?

Hello I am trying to create a regex that recognizes money and numbers
being inputted. I have to allow numbers becuase I am expecting
non-formatted numbers to be inputted programmaticly and then I will format
them mysleves. For some reason my regex is allowing a one letter character
as a possible input.
[\$]?[0-9,]*\.[0-9][0-9]
I understand that my regex excepts the case where multiple commas are
added and and needs two digit after teh decimal point. I had have an idea
to fix that already. I have narrowed it down to possibly the *\. as the
problem
EDIT
I found the regex expression that worked [\$]?([0-9,])*[\.][0-9]{2} but I
still don't know how or why it was failing in the first place
I am using the .formatCurrency() to format the input into a money format.
It can be found here but it still allows me to use alpha characters so i
have to further masked it using the $(this).inputmask('Regex', { regex:
"[\$]?([0-9,])*[\.][0-9]{2}" }); where input mask is found here and
$(this) is a reference to a input element of type text. My code would look
something like this
<input type="text" id="123" data-Money>
//in the script
.find("input").each(function () {
if ($(this).attr("data-Money") == "true") {
$(this).inputmask('Regex', { regex:
"[\$]?([0-9,])*[\.][0-9]{2}" });
$(this).on("blur", function () {
$(this).formatCurrency();
});
I hope this helps. I try creating a JSfiddle but Idk how to add external
libraries/plugin/extension

No comments:

Post a Comment