Here is the modified function:
Code:
function validateForm(theForm) {
var elArr = theForm.elements;
for(var i = 0; i < elArr.length; i++) {
if [!](elArr[i].type == "text")[/!] {
if (/^((\d+(\.\d{1,2})?)|(\d*(\.\d{1,2}){1}))$/.test(elArr[i].value)) {
//if (parseFloat(elArr[i].value) > maxLimit || parseFloat(elArr[i].value) < minLimit) {
// alert(elArr[i] + " must be between x and y, please enter a new value");
// elArr[i].focus();
// return false;
//}
}
else {
alert(elArr[i].value + " is not formatted correctly, please enter a new value");
elArr[i].focus();
return false;
}
}
}
return true;
}
I put the highlighted check in because you don't want to perform the validation check on the button.
I had to split the regexp up with an or clause. This is the reason why:
Your old regexp:
/\d*(\.\d{1,2})?/
When split up is a 2 part process
[ol][li]
\d* means 0 or more numbers[/li]
[li]
(\.\d{1,2})? means 0 or 1 instances of a decimal followed by 1 or 2 numbers.[/li][/ol]
When you were running it before passing a string like "asdfasdfsadfsa" it was evaluating #1 and finding 0 numbers. It then evaluated #2 and found 0 instances of a decimal with 2 or more numbers. Therefore the regexp was valid.
So instead, I split it into 2 circumstances:
[ol][li]/^([!](\d+(\.\d{1,2})?)[/!]|(\d*(\.\d{1,2}){1}))$/[/li]
[li]/^((\d+(\.\d{1,2})?)|[!](\d*(\.\d{1,2}){1})[/!])$/[/li]
[/ol]
Part #1 checks for 1 or more numbers followed by 0 or 1 instances of the decimal with 1 or 2 decimal places. #2 checks for 0 or more numbers with exactly 1 instance of a decimal with 1 or 2 decimal places. This way you will never encounter a string that passes for 0 instances of numbers preceding
and following the decimal. It has to be one or the other (or both) in order to pass. Additionally, I added the beginning (^) and end ($) of string operators to ensure that a string like
1234.12asdf didn't pass.
-kaht
[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
![[banghead] [banghead] [banghead]](/data/assets/smilies/banghead.gif)
[small]
(He's back)[/small]