Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-21-2009, 09:51 PM   PM User | #1
mixelplik
New Coder

 
Join Date: Feb 2009
Posts: 11
Thanks: 5
Thanked 0 Times in 0 Posts
mixelplik is an unknown quantity at this point
Unhappy Validating a entry (first name last)

This is driving me bonkers. I want the user to enter the first and last name in a text field, it will only accept the entry is there are no spaces at the end or begining of the entry and at least one in the middle. This was tricky as I'm a newb, but then I added the part that makes sure only letters were added (supposed to be the easy part) and the whole thing went kerplunk, I know there's a conflict somewhere, just not knowlageable enough to figure out where, can any one help?

Here's my code:

function nm(){
var name = document.getElementById('text').value;
var leng = name.length - 1;

if(name == "" || name.indexOf(" ") == 0 || name.lastIndexOf(" ") == leng || name.match(" ") == null || name.match(/[^a-zA-Z]/)) {

alert('enter a first and a last name');
}else{
document.getElementById("name").innerHTML = name;
}
}

Like I said, everything worked fine until I added the name.match[^a-zA-z] bit. Thanks in advance for any help yo!
mixelplik is offline   Reply With Quote
Old 03-21-2009, 10:21 PM   PM User | #2
freedom_razor
Regular Coder

 
Join Date: Dec 2008
Location: Tannhäuser Gate
Posts: 286
Thanks: 7
Thanked 58 Times in 57 Posts
freedom_razor is an unknown quantity at this point
You forgot about the space between name and surname:
Code:
name.match(/[^a-zA-Z\s]/)
freedom_razor is offline   Reply With Quote
Users who have thanked freedom_razor for this post:
mixelplik (03-21-2009)
Old 03-21-2009, 10:34 PM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Why not simply strip leading and trailing spaces?

x = x.replace(/^\s+|\s+$/g,"");

Also note that in Internet Explorer, names and IDs are global variables and thus you should NEVER use a global variable or function name which is the same as an HTML element name or ID. You should also avoid giving names or id's to your variables/functions/arguments/forms words which are JavaScript methods/properties/attributes such as 'name' or 'id' or 'value' or 'text' or 'checked' or 'submit' or 'replace' or 'button' or 'radio' or 'parseInt'. And of course you may not give a variable a name which is a Javascript keyword or event such as alert, case, char, false, int, onload, return, this, void, and so on.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
mixelplik (03-21-2009)
Old 03-21-2009, 10:58 PM   PM User | #4
mixelplik
New Coder

 
Join Date: Feb 2009
Posts: 11
Thanks: 5
Thanked 0 Times in 0 Posts
mixelplik is an unknown quantity at this point
Thanks! One of the things I love about looking over these boards is different approaches to the same problem, I didn't think of this LOL duh! Great tip!!!
mixelplik is offline   Reply With Quote
Old 03-21-2009, 11:00 PM   PM User | #5
mixelplik
New Coder

 
Join Date: Feb 2009
Posts: 11
Thanks: 5
Thanked 0 Times in 0 Posts
mixelplik is an unknown quantity at this point
Worked like a charm!! thanks!
mixelplik is offline   Reply With Quote
Old 03-22-2009, 10:09 AM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Your heading Validating a entry (first name last) suggests you want to reverse the order of first and last names. This will do that for you:-


Code:
str = "John Smith"
str = str.replace(/(\S+)\s+(\S+)/,"$2 $1");
alert (str);
As well as leading and trailing spaces you may wish to strip out non-alpha characters:-

str = str.replace(/[^a-z-']/gi,""); // retain hyphen and apostrophe for Peter O'Toole and John Smith-Jones

Last edited by Philip M; 03-22-2009 at 10:13 AM..
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:06 AM.


Advertisement
Log in to turn off these ads.