Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

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
Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc