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 11 1160
<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
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 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 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
....yikes.... #:-o
* delimiter *
RobB wrote: ...yikes.... #:-o
* delimiter *
delimeter: Measures popularity of sandwich shops...
Mick
Mick White wrote: RobB wrote:
...yikes.... #:-o
* delimiter *
delimeter: Measures popularity of sandwich shops... Mick
No, no, no...weighs the pastrami, bro' ! #:-D
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!
> 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.
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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"
|
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"
|
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
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
| |