|
|
 |
 |
 |
 |
Javascript / Client Side Development
|
 |
 |
 |
 |
 |
 |
 |
 |
what is the reg exp to do this?
Hi, I want to create a regular expression that will take a single input name field and save the first and last names to two separate js vars. The criteria for determining what is the separation would be the first occurrence of a space (ascii code = 32). However, note that a user may only enter one word or nothing at all. If the DOM id for the field I want to parse is "nameField", how would I do this? Thanks, - Dave
laredotorn@zipmail.com said the following on 5/23/2007 5:03 PM: > Hi, > I want to create a regular expression that will take a single input > name field and save the first and last names to two separate js vars. > The criteria for determining what is the separation would be the first > occurrence of a space (ascii code = 32). However, note that a user > may only enter one word or nothing at all. If the DOM id for the > field I want to parse is "nameField", how would I do this?
Why are you making it harder than it has to be? var theNames = refToFormElement.split(' '); If theNames has a length of 1, then there was no space. If theNames has a length of 2, then there was a space. If theNames has a length > 2, then there was more than one space. -- Randy Chance Favors The Prepared Mind comp.lang.javascript FAQ - http://jibbering.com/faq/index.html Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
On May 23, 3:03 pm, "laredotorn@zipmail.com" <laredotorn @zipmail.com> wrote: > Hi, > I want to create a regular expression that will take a single input > name field and save the first and last names to two separate js vars. > The criteria for determining what is the separation would be the first > occurrence of a space (ascii code = 32). However, note that a user > may only enter one word or nothing at all. If the DOM id for the > field I want to parse is "nameField", how would I do this? > Thanks, - Dave
var fullname=document.getElementById("nameField"); var firstSpace=fullname.indexOf(' '); WScript.Echo( "First Name:"+fullname.substr(0,firstSpace) +"\nLast Name:"+fullname.substr(firstSpace+1) )
On May 23, 4:28 pm, "scripts.contact" <scripts.cont@gmail.com> wrote: > var fullname=document.getElementById("nameField"); > var firstSpace=fullname.indexOf(' '); > WScript.Echo( > "First Name:"+fullname.substr(0,firstSpace) > +"\nLast Name:"+fullname.substr(firstSpace+1) > )
alert( "First Name:"+fullname.substr(0,firstSpace) +"\nLast Name:"+fullname.substr(firstSpace+1) )
laredotorn @zipmail.com wrote: > Hi, > I want to create a regular expression that will take a single input > name field and save the first and last names to two separate js vars. > The criteria for determining what is the separation would be the first > occurrence of a space (ascii code = 32). However, note that a user > may only enter one word or nothing at all. If the DOM id for the > field I want to parse is "nameField", how would I do this? > Thanks, - Dave
A= String.split(/\s+/);AA=A.length; firstName=AA<2?"":A[0]; lastName=AA==1?A[0]:AA>1?A.slice(1).join(" "):"Empty"; Mick
|
 |
 |
 |
 |
|