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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

validation.

Status
Not open for further replies.

WannaLearn

Programmer
Jul 10, 2001
210
0
0
US
Hi, is this doable? I want to do a validation to check if the user entered 3 of the same numbers or 3 digits in a sequence, and 4 of the same numbers back-to-back, and 4 digits in a sequence.
EX: 111 (do not want)
123 (do not want)
156 (ok)
3333 (do not want)
5678 (do not want)
1568 (ok)

Thanks.

 
Try this:

NOTE: myVar is the value you are checking.

Code:
var okay = false;
if(myVar.length == 3 || myVar.length == 4) [b]//only interested in numbers of these lengths[/b]
{
 var temp = myVar;
 while(temp.length > 1) [b]//need at least 2 digits for a test[/b]
 {
  var first = temp.substring(0,1); [b]//first digit[/b]
  var second; [b]//second digit[/b]
  if(temp.length == 2)
   second = temp.substring(1); 
  else
   second = temp.substring(1,2);

  if(first != second) [b]//else, if there are more digits, check them[/b]
  {
   if(parseInt(second) - parseInt(first) != 1) [b]//else, if there are more digits, check them[/b]
   {
    okay = true; [b]//number passes test[/b]
    break;
   }//end if
  }//end if

  temp = temp.substring(1); [b]//advance 'pointer' to next digit[/b]
 }//end while
}//end if

[b]//At this point, [i]okay[/i] is true if number passes criteria and false otherwise[/b]

'hope that helps.

--Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top