473,414 Members | 1,626 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,414 software developers and data experts.

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

May 23 '07 #1
4 1213
la***********@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/
May 23 '07 #2
On May 23, 3:03 pm, "laredotorn...@zipmail.com"
<laredotorn...@zipmail.comwrote:
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)
)

May 23 '07 #3
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)
)

May 23 '07 #4
la***********@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
May 24 '07 #5

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

Similar topics

8
by: Randell D. | last post by:
I have just recompiled, upgraded to PHP 4.3.4. As an exercise (and curiosity) I've decided to test out PDF functions and got the test in the PHP online manual working. I had one problem whereby...
2
by: thecrow | last post by:
Alright, what the hell is going on here? In the following code, I expect the printed result to be: DEBUG: frank's last name is burns. Instead, what I get is: DEBUG: frank's last name is...
220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
9
by: Martin Maney | last post by:
In my copious spare time I've been dabbling at getting a computerized version of a board game working. After deciding that tk just made me want to vomit, and wx was like swimming through...
92
by: Reed L. O'Brien | last post by:
I see rotor was removed for 2.4 and the docs say use an AES module provided separately... Is there a standard module that works alike or an AES module that works alike but with better encryption?...
3
by: Ron_Adam | last post by:
Ok... it's works! :) So what do you think? Look at the last stacked example, it process the preprocess's first in forward order, then does the postprocess's in reverse order. Which might be...
12
by: Dario | last post by:
The following simple program behaves differently in Windows and Linux . #include <stdexcept> #include <iostream> #include <string> using namespace std; class LogicError : public logic_error {...
30
by: James Conrad StJohn Foreman | last post by:
After 3 years of using DB2 on Linux, I'm leaving my current employers to go work for a SQL Server shop instead. In order to find my replacement, they're trying to put together a set of questions...
2
by: yogesh | last post by:
char TCGI::x2c(char *what) { register char digit; digit = (char) ((what >= 'A' ? ((what & 0xdf) - 'A')+10 : (what - '0'))); digit *= (char) 16; digit += (char) ((what >= 'A' ? ((what & 0xdf) -...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.