check user input for float

  • Thread starter Georg Vassilopulos
  • Start date
G

Georg Vassilopulos

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
 
E

Evertjan.

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?
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
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.
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
JRS: In article <[email protected]>, seen in
Georg Vassilopulos <[email protected]>
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top