Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > check user input for float

Reply
Thread Tools

check user input for float

 
 
Georg Vassilopulos
Guest
Posts: n/a
 
      07-04-2004
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


 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      07-04-2004
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)
 
Reply With Quote
 
 
 
 
Dr John Stockton
Guest
Posts: n/a
 
      07-04-2004
JRS: In article <cc8s4o$eef$05$(E-Mail Removed)-online.com>, seen in
news:comp.lang.javascript, Georg Vassilopulos <(E-Mail Removed)>
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.
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      07-05-2004
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)
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      07-05-2004
JRS: In article <(E-Mail Removed)>, seen in
news:comp.lang.javascript, Dr John Stockton <(E-Mail Removed)>
posted at Sun, 4 Jul 2004 19:46:26 :
>JRS: In article <cc8s4o$eef$05$(E-Mail Removed)-online.com>, seen in
>news:comp.lang.javascript, Georg Vassilopulos <(E-Mail Removed)>
>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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
How to check if a String input is integer, float or NAN kt Java 3 04-22-2005 03:00 PM
need code to convert float format to internal java float format which is kept in 4 bytes integer Andy Java 7 05-10-2004 09:26 PM
static_cast<float>(a) versus float(a) Jim West C++ 4 01-16-2004 12:36 PM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments