...

View Full Version : REGEXP to verify first chars



gate
08-01-2002, 01:43 PM
Hi Guys, thanks for spending the time to read my post.

Outline of what im trying to do:

a user enters a string of six chars/numbers into a text box...
The script checks if the first three of those six are equal to a certain value...
If they are NOT the same it throws up a alert() box..
If they are the same the script throws up a different alert() box..

You help is much appreciated..

Best Regards..

nolachrymose
08-01-2002, 02:23 PM
function isValid(string,cString) {
if(string.substring(0,3)==cString) alert("Valid!");
else alert("Invalid!");
}

Hope that helps!

Happy coding! :)

mordred
08-01-2002, 05:33 PM
Originally posted by gate
The script checks if the first three of those six are equal to a certain value...


Apart from the solution noloachrymoose posted, I'll think you have to define more precisely what you mean by "certain value". Since you asked about a RegExp validation routine, I'll suppose you'll expect a certain pattern that must be true for the first three characters, such as "valid are only numerical digits and lowercase letters of the english alphabet".

If that's the case, perhaps this code snippet might help you:



// str is the content of your text field
if (!str.match(/^[0-9a-z]{3}/)) {
alert("invalid");
}


You can also enhance the above routine so that you see exactly what was matched and work upon that further in your code.

Hope that helps.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum