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

Numbers only validation using regular expression

Hi,

I have seen various online resources and tuturials. I ahve written a piece of code but it does not seem to work

[code: text]
Problem defined:
If not number entered (as not mandartory)
ignore
else
if yes then remove the spaces
remove the hypens
check if the values entered now are only numbers using regualr expression
if not, then return fasle
else return true
[code: text]

The bit of Javascript I have written:
[code:javascript]

fucntion checkDetails()
{
var contactNumber=document.frmUser.contactNumber.value ;

if (contactNumber.length != 0)

{
var telnum = contactNumber;

// Remove spaces from the telephone number to help validation
while (telnum.indexOf(" ")!= -1)
{
telnum = telnum.slice (0,telnum.indexOf(" ")) + telnum.slice (telnum.indexOf(" ")+1);
}

// Remove hyphens from the telephone number to help validation
while (telnum.indexOf("-")!= -1)
{
telnum = telnum.slice (0,telnum.indexOf("-")) + telnum.slice (telnum.indexOf("-")+1);
}

// Now check that all the characters are digits
exp = /^[0-9]$/;
if (exp.test(telnum) != true)
{
alert("Please enter only numbers with area code - For example: 01382000000");
}
return false

}
return true

}

[code: javascript]

Now if I enter nothing it works if otherwise it just doesnt add and keeps poping the alreat message even if I have only entered numbers.

Gone through my untrained eyes several time but not able to find the culprit bit.

Would be great if someone can point me out to it.

Thanks in advance
Sree
Jun 20 '07 #1
4 21004
acoder
16,027 Expert Mod 8TB
Your regular expression needs to be /^\d+$/ (notice the +) - see link. You can also use the replace method (described in that link) to remove strings and hyphens instead of your method.
Jun 20 '07 #2
Thanks acoder,

It works, I was using the wrong regular expression, thanks for pointing out...

The tutorial was also very helpful.

Thanks once again,
Sree
Jun 21 '07 #3
acoder
16,027 Expert Mod 8TB
No problem. You're welcome. Post again if you have any more problems.
Jun 21 '07 #4
Nope just one change and the whole code was working ;)

Thanks
Jun 21 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Geoff Soper | last post by:
I've been looking for a simple way of checking that a string is an e-mail address. I don't need to check if the address exists, just if the format of the string matches. There seem to be lots of...
3
by: Skippytpe | last post by:
Does anyone have an idea why the form validation in the following page wouldn't be working? I had been using XHTML 1.0 transitional which allowed me to use the form attribute 'name.' I could then...
3
by: Mark | last post by:
To validate the length of a multiline textbox, I'm told that I have to use a regular expression validator. The regular expression below limits it to 25 characters in length, but if the user enters...
2
by: Nazir | last post by:
Hi I'm using a regular expression validator, but if spaces are entered, it bypasses the validation! I'm using ^{5,100}$
2
by: Coleen | last post by:
I can't remember the correct way to write a custom regular expression validator, and the help file for VB.net is not terribly helpful...it does not give any real world examples. I've got the main...
5
by: JIM.H. | last post by:
Hello, I have this validation expression: ^(?:(?:0?|1)|(?:0?|11)(?!\/31)|(?:0?2)(?:(?!\/3|\/29\/(?:(?:0||)00|(?:\d{2}(?:0||))))))\/(?:0?||3)\/\d{4}$ This is supposed to match MM/DD/YYYY it is...
1
by: Jim Dornbush | last post by:
Has anyone seen an updated regex expression from Microsoft for the email validation expression so that single quotes are allowed? I've been using the canned regex for emails, but recently been...
2
by: jack | last post by:
Im a new bee in validation Im just in the design phase of the project and not thinking about having a saperate validaion block. Just browsed through validaion block of enterprise library which...
1
by: neoupadhyay | last post by:
Why my regular expression validation is not working Friends I am using asp.net 1.1 and i try to apply regular expression validation. when i run it at my local host its work cool, but when i upload...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.