473,386 Members | 1,775 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,386 software developers and data experts.

Test for digit, regular expression

Hello -

I have a form field the results from which should not start with a
digit. I have tried various permutations of the following without
success. Can anyone help me out?

//does not return false when it should
(document.my_form.my_field.value.substring(0) == / \d /)

TIA

Dave Miller
Jul 20 '05 #1
5 6547
Dave Miller wrote on 17 okt 2003 in comp.lang.javascript:
I have a form field the results from which should not start with a
digit. I have tried various permutations of the following without
success. Can anyone help me out?

//does not return false when it should
(document.my_form.my_field.value.substring(0) == / \d /)


It should NOT. Please read up on Reg Ex.

=======================

If (/\d/.test(document.my_form.my_field.value.substring(0) ))

This will be true if the string contains at least 1 digit.

========================

btw:

document.my_form.my_field.value.substring(0)

and

document.my_form.my_field.value

are equivalent

========================

Not tested.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #2
Ivo
"Dave Miller" <un*****@unknown.com> wrote in message
news:MP************************@news.pa.comcast.gi ganews.com...
Hello -

I have a form field the results from which should not start with a
digit. I have tried various permutations of the following without
success. Can anyone help me out?

//does not return false when it should
(document.my_form.my_field.value.substring(0) == / \d /)

TIA

Dave Miller


Remove the spaces from the regex, so
/ \d /
becomes
/\d/
That may give some very different results. Additionally,
/^\d/
will match any string that starts with a digit.

substring(0) returns the whole string, so you might as well use
document.my_form.my_field.value
instead of
document.my_form.my_field.value.substring(0)

You probably meant charAt(0), which is the first and nothing but the first
character, but using the ^ inside the regex should do the trick anyways in
this case.
Ivo
Jul 20 '05 #3
In article <MP************************@news.pa.comcast.gigane ws.com>,
un*****@unknown.com says...
Thank you both for your help.
Jul 20 '05 #4
why not using just a regular expression?
Sort of:

/^\d+/.test(document.formName.fieldName.value);

if it returns true, it means there are lweading digits. That's the most
practical way, uses the built in method test() which requires a reg expr as
its leading object. The regexp in case flags the following:
^ start of line
\d a digit
+ one or plus times

http://www.unitedscripters.com/

"Dave Miller" <un*****@unknown.com>

I have a form field the results from which should not start with a
digit. I have tried various permutations of the following without
success. Can anyone help me out?

//does not return false when it should
(document.my_form.my_field.value.substring(0) == / \d /)

Jul 20 '05 #5
JRS: In article <MP************************@news.pa.comcast.gigane ws.co
m>, seen in news:comp.lang.javascript, Dave Miller <un*****@unknown.com>
posted at Fri, 17 Oct 2003 14:39:52 :-

I have a form field the results from which should not start with a
digit. I have tried various permutations of the following without
success. Can anyone help me out?

//does not return false when it should
(document.my_form.my_field.value.substring(0) == / \d /)

The field.value is a string. There are exactly three types of string :
Starting with a digit
Starting with a non-digit
Not starting - i.e. = "" // do not forget this case

To test string S :
OK = !/^\d/.test(S) // does not start with digit
OK = /^\D/.test(S) // starts with non-digit

It seems likely that in practice you have other conditions on the entry;
the first-character test should be combined with the others using a
longer RegExp, e.g.

OK = /^\D.{3,5}$/.test(S) // non-digit; length 4..6

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: championsleeper | last post by:
i am trying to write a script that will: - check an integer value to see if it has a particular pattern (regular expression match) - tell me what the particular pattern is. I am searching...
6
by: WindAndWaves | last post by:
Hi Folks I have inhereted a script that I understand reasonably well, I just do not understand !/^\d+$/.test(el.value) what the hell does that mean? Below is the script (there are really...
6
by: Karel Miklav | last post by:
If I test() string two times in a row like: var re = new RegExp(expression, "gi"); re.test(s1); re.test(s1); only the first test passes. Is this normal? --
2
by: brian | last post by:
Hi, before coming to .NET, I utilized regular expressions mostly in JScript / JavaScript and also in my favorite text editor: TextPad (www.textpad.com) I don't know about JScript/JavaScript, but...
5
by: Hector Martinez | last post by:
Is there any function to know if a char is a digit Thanks in advantage....
3
by: ad | last post by:
I am learning regular expression. Are there any tools to test regular expression?
38
by: Astra | last post by:
Hi All Could somebody please confirm that if I change my JS expression test from: if (!(/^*$/.test(document.form1.fred.value))) to if (!(/^*$/.test(document.form1.fred.value)))
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
1
by: hellboss | last post by:
Hi ! Can u tel me what is the expression (Regular expression) for the a field like Telephone number which Doesnt exceeds 6 digits Ex:999999 d{5} or ()* Can we use this expression , if...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.