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

check user input for float

Hello!

How do I check user input for following formats:

I only want to accept floats or integers written as:

232,45
232
242.45
345
accepted values should be from
100 to 30000 (100.00 - 30000.00)

can anyone help me?

I only need the code inside the checkForm() funktion. The basics are clear.
Thank you all!
Georg
Jul 23 '05 #1
4 11067
Georg Vassilopulos wrote on 04 jul 2004 in comp.lang.javascript:
Hello!

How do I check user input for following formats:

I only want to accept floats or integers written as:

232,45
232
242.45
345
accepted values should be from
100 to 30000 (100.00 - 30000.00)
function checkForm(x){
x = x.replace(/,/.'.')
if (!/^\d{3,5}(\.\d\d)?$/.test(x)) return false
x = +x
if (x<100||x>30000) return false
return true
}
I only need the code inside the checkForm() funktion. The basics are
clear.


Sorry, what basics?
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 23 '05 #2
JRS: In article <cc*************@news.t-online.com>, seen in
news:comp.lang.javascript, Georg Vassilopulos <lu***@NOSPAMweb.de>
posted at Sun, 4 Jul 2004 14:11:03 :
How do I check user input for following formats:

I only want to accept floats or integers written as:

232,45
232
242.45
345
accepted values should be from
100 to 30000 (100.00 - 30000.00)

can anyone help me?

I only need the code inside the checkForm() funktion. The basics are clear.


See <URL:http://www.merlyn.demon.co.uk/js-valid.htm>; I suggest a RegExp

/^[1-9]\d{2,4}([\.,]\d\d)?$/

followed by setting var X = + mycontrol.value
and testing that X <= 3e4 arithmetically.

Checking the upper limit in the RegExp is possible but not reasonable.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> JL / RC : FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #3
Dr John Stockton wrote on 04 jul 2004 in comp.lang.javascript:
See <URL:http://www.merlyn.demon.co.uk/js-valid.htm>; I suggest a RegExp

/^[1-9]\d{2,4}([\.,]\d\d)?$/
you need the , by . replace here, I think
followed by setting var X = + mycontrol.value
and testing that X <= 3e4 arithmetically.

Checking the upper limit in the RegExp is possible but not reasonable.


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 23 '05 #4
JRS: In article <Tc**************@merlyn.demon.co.uk>, seen in
news:comp.lang.javascript, Dr John Stockton <sp**@merlyn.demon.co.uk>
posted at Sun, 4 Jul 2004 19:46:26 :
JRS: In article <cc*************@news.t-online.com>, seen in
news:comp.lang.javascript, Georg Vassilopulos <lu***@NOSPAMweb.de>
posted at Sun, 4 Jul 2004 14:11:03 :

I only want to accept floats or integers written as:

232,45
232
242.45
345
accepted values should be from
100 to 30000 (100.00 - 30000.00)

can anyone help me?

I only need the code inside the checkForm() funktion. The basics are clear.


See <URL:http://www.merlyn.demon.co.uk/js-valid.htm>; I suggest a RegExp

/^[1-9]\d{2,4}([\.,]\d\d)?$/

followed by setting var X = + mycontrol.value
and testing that X <= 3e4 arithmetically.

Checking the upper limit in the RegExp is possible but not reasonable.


As Evertjan pointed out, substitute dot for comma at some stage, before
conversion to number. If your error message shows the original input,
show that without substitution.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
I find MiniTrue useful for viewing/searching/altering files, at a DOS prompt;
free, DOS/Win/UNIX, <URL:http://www.idiotsdelight.net/minitrue/> Update hope?
Jul 23 '05 #5

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

Similar topics

8
by: hokiegal99 | last post by:
I don't understand how to use a loop to keep track of user input. Could someone show me how to do what the program below does with a loop? Thnaks! ---------------------------- #Write a...
10
by: jeff regoord | last post by:
A user inputs a float value. The scanf() function gets the value. However, I need to create an error handler with an if else statement saying invalid input if the input is not a number. Does...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
1
by: suhdominic | last post by:
#include <iostream> #include <fstream> using namespace std; int main() { cout << fixed << showpoint; float a0,a1,a2,a3,a4,a5,a6,a7,a8; // Nine constant values in the polynomial
5
by: no1zson | last post by:
I have been reading through many of the array questions and cannot find one that addresses my issue. Maybe someone can help me out. Same story, I am learning Java and have just written a CD...
4
by: Tarique | last post by:
Hello All. Is the following good enough to be a safe user input routine? What else should i do to improve it ? #include<stdio.h> #include<stdlib.h> #include<string.h> #define ARRAYSIZE 100 ...
5
by: hammer45 | last post by:
Modify payroll program so that it uses a class to store and retrive the employee's name, hourly rate and the number of hours worked. Use a constructor to initialize the employee information, and a...
3
by: Cheer | last post by:
Hello again, I would like to ask how could I check if the input is right: the program asks user to input a float from a keybord, but what if user inputs a char. Is there any funcion which can...
10
by: Matthias | last post by:
Dear newsgroup. I want to write a template function which accepts either integer or floating point numbers. If a certain result is not a whole number and if the template parameter is an...
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
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...

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.