knowledge-database (beta)

Current group: comp.programming

Javascirpt Help

Javascirpt Help  
smilelots4
 Re: Javascirpt Help  
smilelots4
 Re: Javascirpt Help  
niklasb at microsoft.com
From:smilelots4
Subject:Javascirpt Help
Date:17 Jan 2005 14:56:55 -0800
Hello,

I needed some help in validating the format of a telephone number
(xxx-xxxx). Here is the code i have so far (which also includes other
validations such as "check to see if there is a value in the textbox"
but those work)

function fncTest()
{
x=document.UpdateInput
set=/^\d{3}-\d{4}$/

if (!x.txtPhone.match(set)){
alert( "Please phone number" );
return false;
}
}

Is there something wrong with my syntax?
Thanks
From:smilelots4
Subject:Re: Javascirpt Help
Date:19 Jan 2005 14:50:26 -0800
Still a bit confused, but thanks for the help! :)
From:niklasb at microsoft.com
Subject:Re: Javascirpt Help
Date:18 Jan 2005 10:36:53 -0800
smilelots4 wrote:
>
> function fncTest()
> {
> x=document.UpdateInput
> set=/^\d{3}-\d{4}$/
>
> if (!x.txtPhone.match(set)){
> alert( "Please phone number" );
> return false;
> }
> }

The regular expression looks right, but I don't know what kinds
of objects x and x.txtPhone are. Here are a couple of suggestions
that might help you debug in general.

First, pay attention to error messages. If your problem is a
syntax error the interpreter should tell you this. Likewise
if you're trying to invoke a method that doesn't exist, e.g.,
if the object denoted by x.txtPhone has no match method.

Second, create simple test harnesses to test each piece of
code independently. For example, is the problem your regular
expression or something else? One way to find out is to create
a test harness -- a simple app or web page that does nothing
except exercise your regular expression. Following is an
example, in HTML. If I load the HTML file in Internet Explorer,
I see "true true false false" as expected, which leads me to
believe that your regular expression works, at least for
obvious test cases*.


Test





*about that regular expression, obviously you're making some
assumptions about what a phone number looks like -- for example,
that it's always seven digits. Also, if you really only want to
allow characters '0' through '9', you might want to specify [0-9]
instead of \d because the latter will also match other Unicode
characters which are classified as digits, e.g., the Arabic digts
U+0660 through U+0669.
   

Copyright © 2006 knowledge-database   -   All rights reserved