Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regular expression 1

Status
Not open for further replies.

arst06d

Programmer
Joined
Nov 29, 2002
Messages
324
Hi
Can anyone please tell me whether it is possible to use a regular expression to test both for the format of a value - eg no more than 2 decimal places
Code:
/\d*(\.\d{1,2})?/
as well as validating that it falls within a given range?

Thanks

 
The regular expression you provided checks for any number of numeric characters followed by either no decimal, or a decimal with between 1 and 2 numbers after the decimal. If you're wanting to know if it's possible to check for a numeric range within the regexp - then the answer is yes, but it gets considerably more difficult. I recently did one to see if I could and I ended up needing a ton of or (|) conditions to get it to work. In my opinion, it's just a lot easier to compare the values after you've determined that the value is the correct format.

In case you're curious, here's the regexp I wrote. It checks for a valid number between .00~1 and 8:
Code:
if (!/^((0*[1-8]{1}(\.0+)?)|(0*[1-7]{1}(\.\d+))|(0*\.0*[1-9]+\d*)){1}$/.test(hourAmount)) {

-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] [small](He's back)[/small]
 
blimey!
I think I'll take your advice and check the range seperately.

Cheers
 
errr - looks like I can't even get past the first hurdle - appreciate some advice here.

Code:
    var valid = new Object();

    // REGEX Elements

	//number with max 2 decimals
	valid.NumTo2dp = /\d*(\.\d{1,2})?/;   
    
    
    function validateForm(theForm) 
    {
		var elArr = theForm.elements; 		
		for(var i = 0; i < elArr.length; i++)
		{	
			with(elArr[i])						{              
				var v = elArr[i].validator;		
				if(!v) continue;				
   
				var thePat = valid[v];			
				var gotIt = thePat.exec(value); 
				
			
				if(!gotIt)
				{
					alert(name + ": failure to match " + v + " to " + value);
					return false;
				}
			}
		}				
		return true;
	}

I have an input box with validator="NumTo2dp" but it seems to accept any value entered - ie the variable gotIt is never null.
 
It looks like you're trying to create an object to do the validation when there's really no need. Just set up your validation function like so:
Code:
function validateForm(theForm) {
   var elArr = theForm.elements;         
   for(var i = 0; i < elArr.length; i++) {    
      if (/\d*(\.\d{1,2})?/.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] + " is not formatted correctly, please enter a new value");
         elArr[i].focus();
         return false;
      }
   }
   return true;
}

-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] [small](He's back)[/small]
 
Hi Kaht

I was trying to create a generic form validator - this particular regexp is just the beginning ....

even doing it the way you suggest (ignoring the min & max value at the mo) I still cannot generate the error message no matter whether I input text or leave the input blank.
 
can you post a view-source of your page so that I can create a local copy and try to debug from there?

-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] [small](He's back)[/small]
 
here's the page
Code:
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript src="validator.js">
<!--

//-->
</SCRIPT>


</HEAD>
<BODY>

<form onsubmit="return validateForm(this)">

<INPUT type="text" id="text1" name="text1" validator="NumTo2dp"><br>
<INPUT type="submit" value="Button" id=button1 name=button1>

</form>


</BODY>
</HTML>

and the js

Code:
    // Validator Object
            
    var valid = new Object();

    // REGEX Elements

		//number with max 2 decimals
		valid.NumTo2dp = /\d*(\.\d{1,2})?/;   
    
/*    
    function validateForm(theForm) 
    {
		var elArr = theForm.elements; // get all elements of the form into array
		
		for(var i = 0; i < elArr.length; i++)
		{	
			with(elArr[i])						// for each element of the form...
			{              
				var v = elArr[i].validator;		// get validator, if any
				if(!v) continue;				// no validator property, skip
   
				var thePat = valid[v];			// select the validating regular expr
				var gotIt = thePat.exec(value); // run it on value of elArr[i]
				alert("gotIt: " + gotIt);
			
				if(!gotIt)
				{
					alert(name + ": failure to match " + v + " to " + value);
					return false;
				}
			}
		}				
		return true;
	}
*/
function validateForm(theForm) {
   var elArr = theForm.elements;         
   for(var i = 0; i < elArr.length; i++) {    
      if (/\d*(\.\d{1,2})?/.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] + " is not formatted correctly, please enter a new value");
         elArr[i].focus();
         return false;
      }
   }
   return true;
}
 
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] [small](He's back)[/small]
 
So did that fix it?

-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] [small](He's back)[/small]
 
Sorry Kaht - had to leave the office at a rush last night.
Your solution works perfectly, and I appreciate your explanation of how it works.

Many thanks.
 
No problemo

-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] [small](He's back)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top