...

View Full Version : Regex validation of a surname



rix
09-05-2006, 04:34 PM
Now this is a bit of a sore point for me, seeing as I'm an experienced Java programmer but I'm completely useless at regex. Now I'm supposed to write a javascript validation for a surname that only allowes letters, dash (-), space ( ) and the apostrophe ('). The dash, space or apostrophe is also not allowed to be the first or last characters. Here is some valid surnames and patterns:
O'Connor
MacDonald
Smith-Henderson
van der Merwe
Hill

Here is some of the regex's that I've tried:
/^[a-zA-Z\-\']+$
^[a-zA-Z]+[a-zA-Z ]*$
^([a-zA-Z]+)$

Anyone that can help me with this ? It would be greatly appreciated!

Kor
09-05-2006, 05:39 PM
Try this:


if(mystring.match(/^[ |'|-]/)||!(/^[a-zA-Z- ']*$/.test(mystring))){
alert('Not a valid name')
}

rix
09-07-2006, 08:23 AM
Kor, you are a genius and a savior!
Thanks so much :D

rix
09-11-2006, 09:29 AM
Right, altho Kor definitely put me on the right track, the inevitable happened - they added more specs to it :/

Now, what i've got at the moment is this:

function checkValidChars(pMyValue)
{
if(pMyValue.match(/^[ |'|-]/)||!(/^[a-zA-Z][a-zA-Z- ']*[a-zA-Z]$/.test(pMyValue))){
return false;
}
return true;
}

This is the old spec:

Now I'm supposed to write a javascript validation for a surname that only allowes letters, dash (-), space ( ) and the apostrophe ('). The dash, space or apostrophe is also not allowed to be the first or last characters. Here is some valid surnames and patterns:
O'Connor
MacDonald
Smith-Henderson
van der Merwe
Hill


Now the added on parts are:
-The input is also allowed to be only one character (and that excludes the -, ' and space).
-Check that there's only one ', - or space
-The ', - and space is not allowed to be the first and last character.

Am I asking for too much ?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum