473,378 Members | 1,462 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Need help with regular expression and form field check

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
Jul 23 '05 #1
4 3046
On Tue, 21 Sep 2004 18:18:18 +0300, Tommy
<to*********@REMOVETHISrasku.net> wrote:

[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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2
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");
}
}

Jul 23 '05 #3
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
<M.******@blueyonder.co.invalid> wrote:

[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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #4
Ivo
"Lee" wrote
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 shouldonly be able to enter a string in the format of firstname.lastname or
firstname.initial.lastname. Firstname/initial/lastname can be any charactersbetween a-z and there should be a dot between those. How should the
javascript look like to check this?


Don't forget the infamous O'Briens of this world. Apostrophes, spaces and
some other weird characters are all very possible in a name.
--Iv
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: gunawardana | last post by:
I have to write a program to verify text field in HTML forms. So,I hane to verify a text field with lenth 10 & maxlenth 10.The entered text should be as follows. xxxxxxxxxy where xxxxxxxxx...
3
by: Danny | last post by:
I am trying to do a regular expression to search for a url so anything that has http:\\www.hellothere.com but may not have the http:\\ and may not have the www and may not have http:\\www and...
5
by: John . | last post by:
I am using the Regular Expression Validator control to validate a correct email address. But, at the same time I would like to make it a required field. I tested by using just the regular...
6
by: deepak_kamath_n | last post by:
Hello, I am relatively new to the world of regex and require some help in forming a regular expression to achieve the following: I have an input stream similar to: Slot: slot1 Description:...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.