By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,338 Members | 1,596 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,338 IT Pros & Developers. It's quick & easy.

number field validation - URGENT

P: n/a
Hi...
I have a form with 4 number fields: phone area, phone number, cell
area, cell number.
I did a function that checks the "number" issue for all 4 fields in
the same time (because the code is onBlur: "return ph(this);"). Now
the problem is that if the user hits Enter data still goes to the
database (of course, because I should have the function called in the
form's onSubmit......
But how can I do this because I should now have 4 personalized
functions for each field (maybe) or 1 function that comprise the code
for all 4 fields??!!
I am just lost...I started JavaScript 3 weeks ago and I'm in a
profound agony...
The code looks like this (and maybe even this one has a problem):
function ph(obj){
var checkVal="0123456789";
var objVal=obj.value;
var ok=true;
for(i=0;i<objVal.length;i++)
{
var myChar=objVal.charAt(i);
for(j=0;j<checkVal.length;j++)
if (myChar==checkVal.charAt(j)) break;
if(j==checkVal.length)
{
ok=false;
break;
}
}
if(!ok) //myChar is not in [0,9]
{
alert("This is not a valid value. Please enter a number!");
obj.focus();
return false;
}
return true;
}
Thanks...
Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
simina wrote:
Hi...
I have a form with 4 number fields: phone area, phone number, cell
area, cell number. <snip> The code looks like this (and maybe even this one has a problem):
There is a core javacript function, "isNaN()"
function ph(obj){
return !isNaN(obj);
// "obj" is a poor choice, it suggests an object.
// "userEntry" may be a better choice
}

But I would suggest the use of regular expressions to verify your users'
entries, for instance:
A North American phone #
function isNAPhone(userEntry){
return /^1?{9]$/.test(userEntry.replace(/[^\d]/g,""))
}
However this is a very crude test (an optional 1 followed by 9 numbers,
after non-numerical characters are stripped from the user entry), a
return of "true" does not guarantee a valid phone number (actually, I
don't think it's possible)

Mick function ph(obj){
var checkVal="0123456789";
var objVal=obj.value;
var ok=true;
for(i=0;i<objVal.length;i++)
{
var myChar=objVal.charAt(i);
for(j=0;j<checkVal.length;j++)
if (myChar==checkVal.charAt(j)) break;
if(j==checkVal.length)
{
ok=false;
break;
}
}
if(!ok) //myChar is not in [0,9]
{
alert("This is not a valid value. Please enter a number!");
obj.focus();
return false;
}
return true;
}
Thanks...

Jul 23 '05 #2

P: n/a
Mick White wrote:

<--snip-->
A North American phone #
function isNAPhone(userEntry){
return /^1?{9]$/.test(userEntry.replace(/[^\d]/g,""))
}
However this is a very crude test (an optional 1 followed by 9 numbers,
after non-numerical characters are stripped from the user entry), a
return of "true" does not guarantee a valid phone number (actually, I
don't think it's possible)


You could test it to see if it falls in the acceptable pattern, but to
tell if its a valid # or not, you would have to call it to see if it
gets answered :)

The Area Code got changed recently. At one time, it was always a 0 or 1
for the 2nd digit, I know of a 334 (Alabama). It also never starts with
a 0 or 1. So not sure what the exact rule is now :-\

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #3

P: n/a
On 31 Aug 2004 21:01:47 -0700, simina <sp******@yahoo.com> wrote:
I have a form with 4 number fields: phone area, phone number, cell area,
cell number.
I did a function that checks the "number" issue for all 4 fields in the
same time (because the code is onBlur: "return ph(this);").
Do not use the blur event to validate a field, especially when coupling it
with the focus() method. At best, it traps the user in that control and at
worst, it can lead to a deadlock where the user is bounced back and forth
between two fields. Use the change event.

Also, neither the blur event, nor the change event, can be cancelled.
Returning the result of ph() in such cases it pointless.
Now the problem is that if the user hits Enter data still goes to the
database (of course, because I should have the function called in the
form's onSubmit......
You should always be validating on the server. Javascript validation
should just prevent a round trip whenever possible.
But how can I do this because I should now have 4 personalized functions
for each field (maybe) or 1 function that comprise the code for all 4
fields??!!


The answer depends on the nature of the format for each field. The fields
should be tested with a regular expression, which is far more powerful
(and quicker) than your code. If two or more fields share the same format,
then they should share the same function. You'll need to specify the exact
formats you expect if you need help in this matter.

In the final form validation check, you would simply call all the
functions, returning false if any failed.

If you just want the fields to contain numbers only, your ph() function
can be reduced to:

function ph(elem) {
if(!/^\d+$/.test(elem.value)) {
alert('This is not a valid value. Please enter a number.');
elem.focus();
}
}

<input type="text" ... onchange="ph(this);">

[snip]

Good luck,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #4

P: n/a


simina a écrit:
Hi...
I have a form with 4 number fields: phone area, phone number, cell
area, cell number.
I did a function that checks the "number" issue for all 4 fields in
the same time (because the code is onBlur: "return ph(this);"). Now
the problem is that if the user hits Enter data still goes to the
database (of course, because I should have the function called in the
form's onSubmit......
But how can I do this because I should now have 4 personalized
functions for each field (maybe) or 1 function that comprise the code
for all 4 fields??!!
I am just lost...I started JavaScript 3 weeks ago and I'm in a
profound agony...
The code looks like this (and maybe even this one has a problem):
function ph(obj){
var checkVal="0123456789";
var objVal=obj.value;
var ok=true;
for(i=0;i<objVal.length;i++)
{
var myChar=objVal.charAt(i);
for(j=0;j<checkVal.length;j++)
la condition j<checkVal.length

if (myChar==checkVal.charAt(j)) break;
if(j==checkVal.length)
est en contradiction avec j==checkVal.length.
En clair j==checkVal.length n'est jamais satisfait.

GR
{
ok=false;
break;
}
}
if(!ok) //myChar is not in [0,9]
{
alert("This is not a valid value. Please enter a number!");
obj.focus();
return false;
}
return true;
}
Thanks...


Jul 23 '05 #5

P: n/a
JRS: In article <na********************@twister.nyroc.rr.com>, dated
Wed, 1 Sep 2004 04:24:51, seen in news:comp.lang.javascript, Mick White
<mw******@BOGUSrochester.rr.com> posted :
simina wrote:
Hi...
I have a form with 4 number fields: phone area, phone number, cell
area, cell number.<snip>
The code looks like this (and maybe even this one has a problem):


There is a core javacript function, "isNaN()"
function ph(obj){
return !isNaN(obj);
// "obj" is a poor choice, it suggests an object.
// "userEntry" may be a better choice
}

But I would suggest the use of regular expressions to verify your users'
entries, for instance:
A North American phone #
function isNAPhone(userEntry){
return /^1?{9]$/.test(userEntry.replace(/[^\d]/g,""))
}


That tests for phone numbers in indigenous US/CA land-line format. But,
AIUI, North America also includes Mexico - do they follow that format?
And there are phones in NA which are not indigenous; maybe Jim's mobile.

My newspaper tells me of those in the Netherlands who give up land-line
phones, preferring a combination of broad-band Internet and mobile phone
/* ? Does that give reliable access to emergency services, such as 999
or 112 ? */. So an indigenous customer may not have a land-line number.
However this is a very crude test (an optional 1 followed by 9 numbers,
after non-numerical characters are stripped from the user entry), a
return of "true" does not guarantee a valid phone number (actually, I
don't think it's possible)


On a practical definition of valid, it's not possible. A valid phone
number can be converted to an unusable one, at least short-term, by
burning down a house. But I imagine that numbers starting with the
digits of a current special service, such as 1129999999, can only become
valid as ordinary numbers in the rather distant future.

ISTM that phone numbers can be checked only as being all-numeric (after
stripping likely separators such as () - etc.) and at least a minimum
length.

--
© John Stockton, Surrey, UK. ??*@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.

In MS OE, choose Tools, Options, Send; select Plain Text for News and E-mail.
Jul 23 '05 #6

P: n/a
Dr John Stockton wrote:
JRS: In article <na********************@twister.nyroc.rr.com>, dated
Wed, 1 Sep 2004 04:24:51, seen in news:comp.lang.javascript, Mick White
<mw******@BOGUSrochester.rr.com> posted : <snip>
A North American phone #
function isNAPhone(userEntry){
return /^1?{9]$/.test(userEntry.replace(/[^\d]/g,""))
}

That tests for phone numbers in indigenous US/CA land-line format. But,
AIUI, North America also includes Mexico - do they follow that format?
Mexico's format is quite different. Puerto Rico and some of the
Carribean nations follow the US/Can format.
Mick
And there are phones in NA which are not indigenous; maybe Jim's mobile.


Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.