473,396 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How do I change this code

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
11 1160
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
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
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
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
....yikes.... #:-o

* delimiter *

Jul 23 '05 #6
RobB wrote:
...yikes.... #:-o

* delimiter *


delimeter: Measures popularity of sandwich shops...
Mick
Jul 23 '05 #7
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
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
> 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Patrick Kowalzick | last post by:
Dear NG, I would like to change the allocator of e.g. all std::strings, without changing my code. Is there a portable solution to achieve this? The only nice solution I can think of, would be...
12
by: Arno R | last post by:
Hi there, I just distributed an application in which I (try to) change db.properties depending on CurrentUser() For instance I set the property's AllowBypassKey and AllowBuiltinToolbars to False...
3
by: Tom | last post by:
I am writing a Visual basic .Net database application. There are many forms that first let you select and look at a DB record and then when you click a "modify" button you are allowed to change...
5
by: Elena | last post by:
I need the VB.NET code to change the header/footer of an Excel spread sheet, ASAP. I am doing it through a VB application. I can change the ranges/cell values using code, but I do not know how to...
11
by: Simon | last post by:
Dear reader, The syntax for the VBA code to change the RowSource of a Master Report is: Me.RowSource = "TableOrQueryName"
3
by: Simon | last post by:
Dear reader, The syntax for the VBA code to change the RecordSource of a Master Report is: Me.RecordSource = "TableOrQueryName"
25
by: Peng Yu | last post by:
Hi, It is possible to change the length of "\t" to a number other than 8. std::cout << "\t"; Thanks, Peng
4
by: Heikki Toivonen | last post by:
I was debugging M2Crypto function written in C which changed behavior between Python 2.6 and earlier Python versions. In an error condition the function was supposed to raise exception type A, but...
15
by: Sunny | last post by:
Hi, I can change the lement opacity in IE using. abc.style.filter = 'alpha(opacity=' + 10 + ')'; But this dont work in firefox, In firefox it throws error. How I can change the opacity of an...
2
by: bemytthm | last post by:
I just want to ask abt communicate with AD using ASP.net. I would want to ask you all to help me correct a problem like this: This is a code i use to change password on AD public bool...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.