Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Help with Javascript - Numeric Entry Check

Explorer ,
Jun 06, 2008 Jun 06, 2008
I am struggling with a javascript and would like to ask for help from the community.

I have a form with several currency fields.. Its a very big form, so i wanted to check the user entry when the user moves to the next field using onBlur event, so that the user is not alerted of bad entries at the end.

VALID entires include:

0 to 10,
negative numbers
Decimal entries 55,555.66
$ 1,00,000
Blank entries (only if the user has marked the form as incomplete)

I have attached this test code for your reference. The problem with the code is that that it throws error when user enters for example 5,00,000 (commas)... or $5,000

If the user marks the form COMPLETE, then it should do a final check to check all the entries.

Can someone please help me with this?
779
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 06, 2008 Jun 06, 2008
I had to do this once, and found things like NaN or parsefloat to be unreliable. So I wrote my own function.

function isFloat(theString) {
/*
returns true if the input string is a float
don't want to use parseFloat because for something like 12.2xs, parseFloat will return 12.2 which is not what we want
*/
var i = 0;
var theLength = theString.length;
var thisChar = "";
var usedDecimals = 0;
var isAFloat = true;

if (isAFloat == true) {
for (i=0; i < theLength; i++) {
thisChar = theString.substring(i, i + 1);
// only allowed one decimal point
if (thisChar == ".") {
usedDecimals++;
}

if (usedDecimals > 1) {
isAFloat = false;
}

if(isAFloat == true && thisChar != ".") {
if (thisChar == "1" || thisChar == "2" || thisChar == "3" || thisChar == "4" || thisChar == "5"
|| thisChar == "6" || thisChar == "7" || thisChar == "8" || thisChar == "9" || thisChar == "0" ){
var x = 1; // dummy statement for do nothhing
} // numeric
else {
isAFloat = false;
}
} // usedDecimals ok
} // looping through the string
} // isAFloat is true
return isAFloat;
} // function is float
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 06, 2008 Jun 06, 2008
Thank you for your response Dan,

I tried the code and got seveal errors...I have very limited knowledge of javascript i could not make it work... can you please resend a detailed code with the form etc? I am sorry that i am asking for spoon feeding...thank you in advance

JT
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 06, 2008 Jun 06, 2008
quote:

Originally posted by: JethroTull
Thank you for your response Dan,

I tried the code and got seveal errors...I have very limited knowledge of javascript i could not make it work... can you please resend a detailed code with the form etc? I am sorry that i am asking for spoon feeding...thank you in advance

JT

It would confuse you more. I just read it again and it confused me. The page containing that function is data entry for a lund and browder chart and the validation was that the sum of two form fields could not exceed a certain maximum. The code I posted was to ensure that the form field was numeric before I tried to do math with it. It gets called by the js function that compares the sum to the allowable max.

Feel free to post questions about any specific line.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 06, 2008 Jun 06, 2008
I bought Dreamweaver CS3 about 2 months ago. I HIGHLY recommend it. The Spry Validation does exactly what you want. You can make a huge form with validation like credit card, email, integer, required... in minutes. I'm sure you can download just the spry validation JS and CSS files somewhere and you'll have all you need for all types of validation.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 06, 2008 Jun 06, 2008
Mark, thank you for the tip. I will look into upgrading soon, but can someone help me with this as i had to show a sample to my boss.

thank you

JT
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jun 06, 2008 Jun 06, 2008
quote:

Originally posted by: MarkWright
I bought Dreamweaver CS3 about 2 months ago. I HIGHLY recommend it. The Spry Validation does exactly what you want. You can make a huge form with validation like credit card, email, integer, required... in minutes. I'm sure you can download just the spry validation JS and CSS files somewhere and you'll have all you need for all types of validation.


+1 vote for Spry. Spry is a free download and can be used without buying Dreamweaver.

http://labs.adobe.com/technologies/spry/home.html
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 07, 2008 Jun 07, 2008
I downloaded free version of Spry. Thank you for the link. I am trying to figure where to start etc.... Any starting help for creating this form would be nice... thanks in advance
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 07, 2008 Jun 07, 2008
LATEST
Still trying to get this thing done.

First: The form is to check numberic values - positive or negative... valid numbers may have commas or decimal values...

Second: the form should check for blank entries only if the user checks the box 'Complete'...

someone please help...
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources