Click to See Complete Forum and Search --> : Validate integer
tripwater
09-08-2003, 02:32 PM
I have a form that has to receive a number. Is the a JS function that can check for this? I am not a good JS programmer (PHP)
My form name is addpriority
the field name is priority
the function I am using so far to validat the form looks like so, but it does not validate the integer yet:
function TheFormCheck()
{
if (document.addpriority.values.value == \"\" || document.addpriority.desc.value == \"\")
{
alert(\"Please fill in the required blue fields to continue.\");
document.addpriority.values.focus();
return false;
}
else
return true;
}
THanks for any help.
Khalid Ali
09-08-2003, 02:46 PM
there are multiple functions in JS that you can use.
parseInt(number);
parseFloat(number);
Number(number);
you can read the JavaScript documentation to get a detailed description about these functions at
devedge.netscape.net
David Harrison
09-08-2003, 03:12 PM
I find that, while those three are useful, they aren't perfect. Which is why I devised this. It removes all non-numbers apart from one decimal point (if there is one) and returns it to the text box. Then when you get the number back from the text box all that is needed is a simple parseFloat.
Khalid Ali
09-08-2003, 03:17 PM
Originally posted by lavalamp
I find that, while those three are useful, they aren't perfect.
LMAO....:D
Netscape should have hired you before writing the JavaScript implementations...
David Harrison
09-08-2003, 03:20 PM
Do I sound like I work for Netscape or something? In case you're wondering, I don't. :)
Khalid Ali
09-08-2003, 03:24 PM
Originally posted by lavalamp
In case you're wondering, I don't. :)
I know....you work for MS...:-) (jking)
Oh BTW
those methods work perfectly for the senearios they are devised,of course people have different needs to and these methods are only meant to provide you with a mean to handle most situations,for custom problems perfect solutions are always those that are custom written..
David Harrison
09-08-2003, 03:31 PM
When I first came accross them I thought that they did just what my script did. How wrong I was.
Anyway tripwater, if you're interested I could probably make a script that uses array's insted of regular expressions that would "seek out" the numbers (and one dp). It would be easier to understand, (better than /\D/g anyway), and would work on ALL browsers that have js enabled, (I think).
Khalid Ali
09-08-2003, 03:34 PM
refexp is suppose to be faster then usign something in arrays..:-)
David Harrison
09-08-2003, 03:39 PM
Having re-read the thread title, I modified the script a little. It's now a lot simpler in the checknum function and only returns intergers to the text box.
tripwater
09-08-2003, 04:24 PM
Thanks that last download works fine for me.
:)
David Harrison
09-08-2003, 04:31 PM
Happy to help. :)
tripwater
09-12-2003, 11:23 AM
Hello,
I had a post earlier in the week in which
Khalid Ali and lavalamp replied. I downloaded Lavalamp's last JS file and used it as it seemed to work fine for me at the time but the problem I seem to be having is (just to catch you up this is a manual hours popup box.) The user will enter 4 or 4.5 or 12 etc. I will convert on submit to integer and insert into DB. Well if the user hits enter key the post_var (PHP) echoes 4.5 if a float is typed in but if the focus leaves the field to hit the submit button itself, the 4.5 hours posts 45 hours.
I guess I should have been more specific in my earlier post I apologize for that. What can I do to rectify this? I basically need to have the ability for an int or float typed into the field and if anything else like special chars or strings --alert.
Thanks and sorry.
David Harrison
09-12-2003, 11:53 AM
Is this what you need:
tripwater
09-12-2003, 02:12 PM
That will do it. Thanks
David Harrison
09-12-2003, 02:22 PM
Happy to help. :)