View Full Version : Validation
PauletteB
08-14-2002, 05:36 PM
In the following expression, I need to declare that the three sets of numbers can only have a value of between 0 and 299. Any value over 299 and the search would return the Void.
if (theCard.search(/^[\d]{1,3}[,|-][\d]{1,3}[,|-][\d]{1,3}$/g) == -1 )
{ document.cForm.oBox.value = "Void" }
Example:
000-299-299 is ok, but 0-0-300 needs to return the Void.
As it stands, any value to and including 999 is being accepted.
How would this be done?
Many thanks,
Paulette
requestcode
08-14-2002, 08:01 PM
Here is a script for checking if an IP address is valid. Maybe you can adopt it for your needs.
<html>
<head>
<title>Validate IP Address</title>
<SCRIPT LANGUAGE="JavaScript">
var validIP =/\d{1,3}[.]\d{1,3}[.]\d{1,3}[.]\d{1,3}/;
function checkip(formobj)
{
var sepnums
var flag=""
if(formobj.ipaddr.value.match(validIP)) // first check for numbers and periods
{
sepnums=formobj.ipaddr.value.split(".") // split on period into separate values
for(i=0;i<sepnums.length;i++)
{
if(sepnums[i]>255) // if greater than 255 set flag to y
{flag="y"}
}
if(flag=="y") // if flag equals y then not valid ip
{alert("Is not Valid!")}
else
{alert("Is Valid")}
}
else // if not numbers or missing periods
{alert("Is not Valid!")}
}
</SCRIPT>
</head>
<body>
<FORM NAME="ipform">
<INPUT TYPE="text" NAME="ipaddr" SIZE="15" MAXLENGTH="15">
<INPUT TYPE="button" onClick="checkip(this.form)" VALUE="Check IP Address">
<FORM>
</body>
</html>
beetle
08-14-2002, 10:12 PM
You need to remember that a regular expression only recognizes strings, so to create a accurate RegExp, you need to view your criteria as a string too/^((?:([0-2]{1}[0-9]{0,2})|([0-9]{1,2}))[,|-]){2}(?:([0-2]{1}[0-9]{0,2})|([0-9]{1,2}))$/She's a doozy, ain't she? :D
Of course, this will match 0-299,299 or 100,100-0. If you want both of the separating characters to be the same in each match, the regex will need to be different.
beetle
08-14-2002, 10:22 PM
Of course, the one for an IP is even worse :D/^((?:([2]{1}[0-5]{2})|([2]{1}[0-4]{1}[0-9]{1})|([1]?[0-9]{2})|([0-9]{1}))[\.]){3}(?:([2]{1}[0-5]{2})|([2]{1}[0-4]{1}[0-9]{1})|([1]?[0-9]{2})|([0-9]{1}))$/
PauletteB
08-15-2002, 10:02 PM
beetle;
Thanks for the prompt replies. The validation worked the first time off with IE, but I had to message it a bit for Netscape(4+), probably due to my form.
Great stuff! Learned alot about the structure of validation. My second validation was basically the same format, except the max value had to be 255, and after a few hours, got that one going. That was fun! ...but it works great. That one is a long one as well, but I take it in stride.
Sure learned lots about structure with your reply.
Again, thanks,
PauletteB
(and thanks also to 'requestcode' for your reply)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.