Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > number field validation - URGENT

Reply
Thread Tools

number field validation - URGENT

 
 
simina
Guest
Posts: n/a
 
      09-01-2004
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...
 
Reply With Quote
 
 
 
 
Mick White
Guest
Posts: n/a
 
      09-01-2004
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...

 
Reply With Quote
 
 
 
 
Randy Webb
Guest
Posts: n/a
 
      09-01-2004
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
 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      09-01-2004
On 31 Aug 2004 21:01:47 -0700, simina <> 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.
 
Reply With Quote
 
G Roydor
Guest
Posts: n/a
 
      09-01-2004


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


 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      09-01-2004
JRS: In article <nacZc.209905$>, dated
Wed, 1 Sep 2004 04:24:51, seen in news:comp.lang.javascript, Mick White
<> 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.
 
Reply With Quote
 
Mick White
Guest
Posts: n/a
 
      09-01-2004
Dr John Stockton wrote:

> JRS: In article <nacZc.209905$>, dated
> Wed, 1 Sep 2004 04:24:51, seen in news:comp.lang.javascript, Mick White
> <> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript validation for a not required field, field is onlyrequired if another field has a value jr Javascript 3 07-08-2010 10:33 AM
1.Enter space bar for field names and save the field.The field shoud not get saved and an alert should be there as"Space bars are not allowed" Sound Javascript 2 09-28-2006 02:43 PM
copy and paste form RTF document into field in asp form cause it to bypass field length and javascript validation - how to overcome? NotGiven Javascript 3 05-13-2004 12:15 AM
copy and paste form RTF document into field in asp form cause it to bypass field length and javascript validation - how to overcome? NotGiven ASP General 3 05-13-2004 12:15 AM
concatenate in Javascript function? simple field validation funciton not working - needs to concat field name with object NotGiven Javascript 7 07-24-2003 11:44 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57