Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > How do I change this code

Reply
Thread Tools

How do I change this code

 
 
effendi@epitome.com.sg
Guest
Posts: n/a
 
      03-09-2005
A gentleman in this forum gave me this code and it work great to test
that a string consist of only digits and it is 8 character long.

(!/^\d{8}$/.test(ContactNo) )

How do I modify the above if I want the string to be of any length but
consist of only digits?

Thanks

 
Reply With Quote
 
 
 
 
Ivo
Guest
Posts: n/a
 
      03-09-2005
<> wrote
> A gentleman in this forum gave me this code and it work great to test
> that a string consist of only digits and it is 8 character long.
>
> (!/^\d{8}$/.test(ContactNo) )
>
> How do I modify the above if I want the string to be of any length but
> consist of only digits?


Change the three characters "{8}" into "{1,}" or into a single "+" sign.

Or divide by 2 and see if it 's still a number...

Or...
--
Ivo


 
Reply With Quote
 
 
 
 
effendi@epitome.com.sg
Guest
Posts: n/a
 
      03-09-2005
Thanks, the code worked.

I was just wondering, if you could explain how the syntax of the test
is actually constructed? I would be highly appreciative.

Regards

 
Reply With Quote
 
Mick White
Guest
Posts: n/a
 
      03-09-2005
wrote:
> Thanks, the code worked.
>
> I was just wondering, if you could explain how the syntax of the test
> is actually constructed? I would be highly appreciative.
>
> Regards
>

Please quote the prevous posting...


[Regular Expression Here].test("String Here")
returns true or false


alert(/^\d+$/.test("123")) //true
alert(/^\d+$/.test("123a")) //false

if(/^\d+$/.test(yourValue)){
//good input, do stuff with it
} else{
//bad input
}
Mick
 
Reply With Quote
 
RobB
Guest
Posts: n/a
 
      03-09-2005
wrote:
> Thanks, the code worked.
>
> I was just wondering, if you could explain how the syntax of the test
> is actually constructed? I would be highly appreciative.
>
> Regards


Apologies to Ivo...

Regular expression:

/ ---> delimeter (like quotes for a string, marks start & end)
^ ---> pattern must start at very beginning of sample string
\d ---> metacharacter, matches one digit (0-9)
+ ---> quantifier, specifies one or more of the preceding
(and replaces the {8}, which specifies exactly eight;
{3,8}, e.g., would require between 3 and 8; {2,} requires at least two
or more; '*' signifies zero or more, '?' is zero or one (i.e.,
optional)
$ ---> pattern must match to the end of sample string
/ ---> closing delimeter

So: this will match a string that contains one or more digits. And
nothing else. The exclamation mark before the call to test() 'flips'
the result: if the test fails, false becomes true, which triggers the
error message, etc.

http://www.webreference.com/js/column5/index.html

 
Reply With Quote
 
RobB
Guest
Posts: n/a
 
      03-09-2005
....yikes.... #

* delimiter *

 
Reply With Quote
 
Mick White
Guest
Posts: n/a
 
      03-09-2005
RobB wrote:

> ...yikes.... #
>
> * delimiter *
>


delimeter: Measures popularity of sandwich shops...
Mick
 
Reply With Quote
 
RobB
Guest
Posts: n/a
 
      03-09-2005
Mick White wrote:
> RobB wrote:
>
> > ...yikes.... #
> >
> > * delimiter *
> >

>
> delimeter: Measures popularity of sandwich shops...
> Mick


No, no, no...weighs the pastrami, bro' ! #

 
Reply With Quote
 
Got Scripting?
Guest
Posts: n/a
 
      03-10-2005
> Thank you for the explanation. It does seem that this test is only to
> determine that a string contains only numbers. Can I use this test to
> make sure that a string contain alphabets and numbers with no special
> character?


You can use "a test" (not "this test"), a "Regular Expression" test, to
do what you want, and more. All you have to do is learn about "Regular
Expressions". You've been pointed in the right direction. Now you can
google for tutorials on the topic.

 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      03-10-2005
Effendi Baba wrote:
> Rob
>
> Thank you for the explanation. It does seem that this test is only to
> determine that a string contains only numbers. Can I use this test to
> make sure that a string contain alphabets and numbers with no special
> character?
>


Please don't top-post.

Play with this:

<input type="text" onblur="checkMe(this.value);">

<script type="text/javascript">
function checkMe(x){
alert(!/\W+/.test(x));
}
</script>


\W matches any non-word character (i.e. not a-z A-Z or 0-9).
Putting ! in front negates the test, so if it finds one or more
non-word characters, it returns false.

Here is a reference here to help you learn RegExp:

<URL:http://www.webreference.com/js/column5/>

--
Rob
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Change the master GridView after detail change? Q. John Chen ASP .Net 0 11-15-2006 05:31 PM
Change the master GridView after detail change? Q. John Chen ASP .Net 0 11-15-2006 05:30 PM
A Paradise DNS address change? What change? There was no change. Tony Neville NZ Computing 7 09-22-2006 01:02 PM
how do I dynamically change an html meta tag from my code behind code? ntm ASP .Net 2 01-19-2004 05:00 PM



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