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

How do I change this code

P: n/a
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

Jul 23 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Ivo
<ef*****@epitome.com.sg> 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
Jul 23 '05 #2

P: n/a
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

Jul 23 '05 #3

P: n/a
ef*****@epitome.com.sg 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
Jul 23 '05 #4

P: n/a
ef*****@epitome.com.sg 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

Jul 23 '05 #5

P: n/a
....yikes.... #:-o

* delimiter *

Jul 23 '05 #6

P: n/a
RobB wrote:
...yikes.... #:-o

* delimiter *


delimeter: Measures popularity of sandwich shops...
Mick
Jul 23 '05 #7

P: n/a
Mick White wrote:
RobB wrote:
...yikes.... #:-o

* delimiter *


delimeter: Measures popularity of sandwich shops...
Mick


No, no, no...weighs the pastrami, bro' ! #:-D

Jul 23 '05 #8

P: n/a
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?
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)


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #9

P: n/a
> 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.

Jul 23 '05 #10

P: n/a
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
Jul 23 '05 #11

P: n/a
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?
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 twoor more; '*' signifies zero or more, '?' is zero or one
(i.e., optional)

if (/[^A-Z0-9]/i.test(ContactNo))
{...bad input...

[...] ---> character class, matches any one character in here
A-Z0-9 ---> matches A through Z, zero through nine
^ ---> negates the above: match anything *but* A-Z, 0-9
/i ---> case-insensitive flag, so will (not) match a-z as well

No need to 'flip' the test result, as you're now testing for *invalid*
input...

There's a 'word' metacharacter as well (\w) - and its inverse (\W) for
'anything but a word character' - but they both include the underscore
as well, so the character class is necessary here.

Jul 23 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.