Hey all.
I would like to validate a string but am confused how I can do it. The string must be look like this...
xxxxx-xxxxxx
So there must be 6 charactors infront and after a hyphen. How can I do this??
Tracy
Beagle
03-15-2006, 11:21 PM
use regular expressions:
var reg = '/^.{6}-.{6}$/'
if (str.match(reg))
{
alert('Valid');
}
Philip M
03-16-2006, 08:29 AM
use regular expressions:
var reg = '/^.{6}-.{6}$/'
if (str.match(reg))
{
alert('Valid');
}
Possibly not what Nikko wants. A . (dot) matches any character including a space or a special character such as a bracket.
More likely what is wanted is
var reg = '/^[a-zA-Z]{6}-[a-zA-Z]{6}$/'
i.e. 6 alpha charcaters hyphen 6 alpha characters
You may use a shorthand A-z for alpha. No need of quotes
for alpha
var reg = /^[A-z]{6}-[A-z]{6}$/;
for numbers
var reg = /^\d{6}-\d{6}$/;
for numbers and alpha (but no special characers)
var reg = /^[A-z-\d]{6}-[A-z-\d]{6}$/;