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

regex lastname o'neal or mc clury or doe

Status
Not open for further replies.

meriwether

Programmer
Mar 7, 2003
23
US
trying to get my regex js to validate a last name field on a form.

/^[a-zA-Z\s]+$/;

this allows me to enter a alpha name (doe), or a alpha space alpha name (mc clury). i need to add ' for names such as o'neal but can't seem to get it.

any clues? i've tried:

/^[a-zA-Z\s']+$/;

but this allows only a ' as valid! ugh.

any help appreciated. thx.
 
/^[a-zA-Z\s\x27]+$/;


Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
 
you may want to allow a - for double barrel names.

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
dwarfthrower,

/^[a-zA-Z\s\x27]+$/;

still allows only a ' as a valid entry.

going to try a combo of expressions, like any alpha w/ a ' or space, any alpha, or any alpha, or any alpha space alpha. if i get it right, i'll post it. thx tho for the attempt.

i take it the x27 replaces the '
is there a list showing these replacements? that would be handy.

thnx
 
ok,
this *seems* to be working.

allows:
mc clury
clury
o'neal
but not
123
'
" " (space).

/^(([a-zA-Z\s])|([a-zA-Z]x27[a-zA-Z]+$))/
 
my bad,
it also accepts:
asdf123asdf

:(

well, i'll keep working on it. any help always appreciated.
 
/^(([a-zA-Z\s]+$)|([a-zA-Z\s]+('|-)+[a-zA-Z\s]+$))/

going w/ this, appears to work. allows:

alpha-alpha
alpha alpha
alpha
alpha 'alpha
alpha' alpha
alpha alp'ha (and similar)
alph'a alpha (and similar)
&quot; &quot;alpha (space alpha) < maybe better not to allow..but...

will not allow:
'
&quot; &quot; (space)
'alpha
any numeric value

i *think* good for a firstname or lastname validation.
 
First off here's a very good reference for JScript reg exps:
(see syntax page for list of characters and what they do)

Second, I think DT's reg exp is a bit better - perhaps it wasn't working for you because of how you tested with it? Have a try of this in a new .html file:
Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd&quot;>[/URL]
<html>
<head>
<title>test bed</title>
<script language=&quot;Javascript&quot;>
//var regex = /^(([a-zA-Z\s]+$)|([a-zA-Z\s]+('|-)+[a-zA-Z\s]+$))/;
var regex = /^[a-zA-Z\s'\-]+$/;
function checkIt() {
	var textfld = document.getElementById(&quot;strID&quot;)
	var strCheck = textfld.value;
	var tested = regex.test(strCheck);
	
	alert(tested + &quot; for [&quot; + strCheck + &quot;]&quot;);
	textfld.focus();
}
</script>
</head>
<body>
<p>
regexp check:<br>
<input name=&quot;strCheck&quot; id=&quot;strID&quot; size=&quot;20&quot;> 
<input type=&quot;button&quot; name=&quot;Check&quot; value=&quot;Check&quot; onclick=&quot;checkIt();&quot;>
</p>
</form>
</body>
</html>

One example of your reg exp not working would be something like: Jo-Anne Smith-Bensson or the like. The pattern of the reg exp i'm using above is:
[tt]^[a-zA-Z\s'\-]+$[/tt]

[tt]^ $[/tt]: start-of-string and end-of-string
[tt] [ ] [/tt]: match any ONE character inside these
[tt] + [/tt]: change previous [] to match one or more
[tt] a-zA-Z\s'\- [/tt]: an upper or lowercase letter, space, apostrophe, or hyphen

Hope that clears up a few things for you

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
eh, this works fine for me in IE6, winNT

<script type=&quot;text/javascript&quot;>
function isValid(s) {
return /^[a-z'\s]+$/i.test(s);
}
</script>

returns true for
Smith
Mc Smith
Mc'Smith
Mc'Smith's Mc'Smithy

returns false for
Smith7
Mc-Smith



=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top