Need help with regular expression and form field check

T

Tommy

I am a complete beginner in regular expressions and would need some help
from the group regarding checking a form field.

I have a field on the form where users enters a text string. The user should
only be able to enter a string in the format of firstname.lastname or
firstname.initial.lastname. Firstname/initial/lastname can be any characters
between a-z and there should be a dot between those. How should the
javascript look like to check this?

br,

Tommy
 
M

Michael Winter

On Tue, 21 Sep 2004 18:18:18 +0300, Tommy

[snip]
I have a field on the form where users enters a text string. The user
should only be able to enter a string in the format of
firstname.lastname or firstname.initial.lastname.
Firstname/initial/lastname can be any characters between a-z and there
should be a dot between those. How should the javascript look like to
check this?

If you want to check the format, nothing more, then use:

/^[a-z]+\.[a-z]+(\.[a-z]+)?$/i.test(value)

which will return true for a match, false otherwise.

If you want to extract the values, use:

var r = /^([a-z]+)\.([a-z]+)(\.([a-z]+))?$/i.exec(value);

if(r) {
var first, init, last;

first = r[1];
if(3 == r.length) {
last = r[2];
} else {
init = r[2];
last = r[4];
}
}

By the way, I don't think it's a good idea to get the user to enter their
full name delimited by periods; it's not particularly natural. Two text
boxes with an optional third would be better.

Hope that helps,
Mike
 
L

Lee

Tommy said:
I am a complete beginner in regular expressions and would need some help
from the group regarding checking a form field.

I have a field on the form where users enters a text string. The user should
only be able to enter a string in the format of firstname.lastname or
firstname.initial.lastname. Firstname/initial/lastname can be any characters
between a-z and there should be a dot between those. How should the
javascript look like to check this?

You haven't specified a minimum or maximum length for firstname
or lastname, so "frank.b", "f.baker" and "f.r.b" are all valid.
That might be best, since there may be a few people with single
character names. This does mean that you can't detect when the
user has entered "initial.lastname" or "firstname.initial".

"Frank.R.Baker" is not valid, because you specified a-z. If that
was an oversight, add an "i" after the ending "/" of the RegExp.

function validate(str){
if(-1==str.search(/^[a-z]+(\.[a-z]){0,1}(\.[a-z]+){0,1}$/)){
alert("\""+str+"\"\nis not in the correct format.");
}else{
alert("ok");
}
}
 
M

Michael Winter

After reading Lee's post, I see I made a mistake. Corrections are below.

On Tue, 21 Sep 2004 16:00:00 GMT, Michael Winter

[snip]
If you want to check the format, nothing more, then use:

/^[a-z]+\.[a-z]+(\.[a-z]+)?$/i.test(value)
/^[a-z]+(\.[a-z])?\.[a-z]+$/i.test(value)

[snip]

If you want to extract the values, use:

var r = /^([a-z]+)\.([a-z]+)(\.([a-z]+))?$/i.exec(value);

if(r) {
var first, init, last;

first = r[1];
if(3 == r.length) {
last = r[2];
} else {
init = r[2];
last = r[4];
}
}

var r = /^([a-z]+)(\.([a-z]))?\.([a-z]+)$/i.exec(value);

if(r) {
var first, init, last;

first = r[1];
init = r[3];
last = r[4];
}

where init will be an empty string if no inital was entered.

[snip]

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,128
Latest member
ElwoodPhil
Top