number field validation - URGENT

S

simina

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...
 
M

Mick White

simina said:
Hi...
I have a form with 4 number fields: phone area, phone number, cell
area, cell number.
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...
 
R

Randy Webb

Mick White wrote:

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 :-\
 
M

Michael Winter

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
 
G

G Roydor

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...
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Wed, 1 Sep 2004 04:24:51, seen in Mick White
simina said:
Hi...
I have a form with 4 number fields: phone area, phone number, cell
area, cell number.
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.
 
M

Mick White

Dr said:
JRS: In article <[email protected]>, dated
Wed, 1 Sep 2004 04:24:51, seen in Mick White
<[email protected]> posted :
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top