473,326 Members | 2,124 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,326 software developers and data experts.

Numeric characters only script

Hi folks,

Im having a bit of trouble here with a system i inherited and i now
have to modify. Basically i need to create a javascript function that
checks a field to only allow numbers 0123456789 and a decimal point. I
also want a validation check to throw an error if there are more than
one decimal points in the field.

can anyone help me with this type of function or at least point me in
the direction of a few useful sites that may help. Im new to
javascript so this may b simple to some but its difficult for me.

thanks in advance.
CG
Jul 23 '05 #1
3 5391

Colin Graham wrote:
Hi folks,

Im having a bit of trouble here with a system i inherited and i now
have to modify. Basically i need to create a javascript function that
checks a field to only allow numbers 0123456789 and a decimal point. I also want a validation check to throw an error if there are more than
one decimal points in the field.

can anyone help me with this type of function or at least point me in
the direction of a few useful sites that may help. Im new to
javascript so this may b simple to some but its difficult for me.

thanks in advance.
CG


[\d]*[.]?[\d]*

i just copied this regular expression out of a php script of mine, so
it's not tested in javasript. it allows for:

[\d]* any amount of numbers

[.]? 1 or no dec point

[\d]* any amount of numbers

i've never used regexes in js, so you have to look up the functions
yourself.

bear in mind that validating data with javascript is not a safe way to
validate user data, server side validating is the only safe way.

micha

Jul 23 '05 #2
Colin Graham wrote on 25 apr 2005 in comp.lang.javascript:
Im having a bit of trouble here with a system i inherited and i now
have to modify. Basically i need to create a javascript function that
checks a field to only allow numbers 0123456789 and a decimal point. I
also want a validation check to throw an error if there are more than
one decimal points in the field.

can anyone help me with this type of function or at least point me in
the direction of a few useful sites that may help. Im new to
javascript so this may b simple to some but its difficult for me.


result = /^\d+\.?\d*$/.test(mystring)

returns true for:
123
123.
0.123

false for
..123
1.2.3
123q
123 456

not tested

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #3
Colin Graham wrote:
checks a field to only allow numbers 0123456789 and a decimal point. I
also want a validation check to throw an error if there are more than
one decimal points in the field.


if (!/^\d+(\.\d+)?$/.test(inputRef.value)) {
// error
}

Allows decimal digits, and up to one decimal point that's not in the
first or last position.

ciao, dhgm
Jul 23 '05 #4

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

Similar topics

1
by: JuniorLinn | last post by:
Hi there - I would like to share this strip of code with our SQL 2000 DBA community. The code below strips all non-numeric characters from a given string field and rebuilds the string. Very...
5
by: M. Akkerman | last post by:
Hi, I've been working on this problem for a while now but I just can't seem to find a good solution for it. Here is the situation. I have the following class class TBigNumber {
2
by: anonieko | last post by:
This applies to javascript dynamic textbox onkey > > > Newsgroups: comp.lang.javascript From: Lasse Reichstein Nielsen <l...@hotpop.com> - Find messages by this author Date: Fri, 15 Jul 2005...
3
by: success_ny | last post by:
Does anyone have a code snippet to compare those values so I can sort the array of alpha-numeric values that include both characters and integers in it? I.e., if we have values like 4236 and...
2
by: lightzizo | last post by:
Hello all, Is there any way makes me prevent the user from entering characters in a Text object I want the Textbox to accept only numeric values when writting into it. Thanks Regards,
7
by: bcpkh | last post by:
Hello All I need to check a string to make sure it does not contain any non numeric characters, the problem that I face is that the string is fairly long, 2784601121574585949, strtol etc. can't...
1
by: frasmus | last post by:
*Please excuse me for cross-posting. I really can't find a suitable "wireless internet" group to ask this question, except forum.nokia, where I didn't have any luck. I have a web page for...
8
by: .Net Sports | last post by:
I am checking for text input on a form validation in javascript that required at least one numeric character along with any number of alpha characters for a given input text box. The below is a var...
4
by: Dennieku | last post by:
Hi, I have to develop an on-screen keyboard and on-screen numeric keypad for a touchscreen UI. The hardest thing with this is that it has to be multi-lingual. Has anybody have ideas how to...
5
lotus18
by: lotus18 | last post by:
Hello World! I have a sample code here written in vb .net that restricts the textbox to accept only alpha, alphanumeric or numeric characters. Public Enum MyOption Alpha = 1 ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.