Click to See Complete Forum and Search --> : tweak script to slightly alter what it does...


sanjuT
05-02-2003, 09:21 AM
this script checks if the user has entered 3 letters followed by 6 numbers in the textbox:

<input type="text" name="AccNo" onChange="javascript:
if (!/^[a-z]{2,5}[0-9]{6}$/i.test(this.value)) {
alert('Invalid account number!');
this.value='';
this.focus();
return false;
}
return true;">

How can this script be altered to check if the user has entered a number, and if that number is at least 12 (less than 12 would trigger the alert).

THANKS!!!

Padrill
05-02-2003, 09:47 AM
This code is matching 2 to 5 letters followed by 6 digits (however I'm confused as to what the beginning ! and ending i are there for in the regular expression - can anyone enlighten?)

As to comparing the number, that's done easily:


<input type="text" name="AccNo" onChange="java script:
if (parseInt(this.value)<12) {
alert('Invalid account number!');
this.value='';
this.focus();
return false;
}
return true;">


---------------------

You're welcome :D

pyro
05-02-2003, 09:57 AM
The beginning ! is part of the if statment, meaning 'not' ie. is not equal to the following reg exp. The ending i means to make the reg exp case insensative. ie in alphabetic matches, it will match aaa and AAA

sanjuT
05-02-2003, 10:21 AM
Thanks!

that code works perfectly for me!:D

Padrill
05-02-2003, 10:26 AM
Thanx Pyro! it now seems obvious and I wonder how could I get confused; but it reminded me of the spanish exclamation eg. iHola! so I thought !i was a pair to begin with... One's mind is free to wander.