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

validation on numeric field

Engaged ,
Jun 05, 2015 Jun 05, 2015

Copy link to clipboard

Copied

Hi. Does anyone know how to use javascript? I have a numeric field in my SQL table called Eng_Hrs. All I want to do is to make sure a number goes into this field instead of some text. It can have up to 2 decimal points. I've tried adding validate="float"  and adding validate="numeric", but that doesn't seem to work. I would rather do javascript anyway if I could since it would pop up a window that tells the user to enter a valid number into this field, and the form would not submit until this was entered correctly. I do have some javascript I used on a currency field somewhere else, but this field is just a number and can have 2 decimal points. I'm just not sure what I'm missing and why it's not working. Here's the javascript I have:


<SCRIPT LANGUAGE="JavaScript">

<!-- Original:  Wayne Nolting (w.nolting@home.com) -->

<!-- This script and many more are available free online at -->

<!-- Begin

function verify()

{

var PartNum=document.AddECNumber.Part_Number.value;

var regularExpression = new RegExp(/^[cC][0-9]/); //regular expression to check for a letter C followed by a number

if(regularExpression.test(PartNum)&& document.AddECNumber.CN_Entry_Initials.value == "N" && document.AddECNumber.Validation_Qty.value == "") { //this will return true if the input passes the regular expression

alert("Enter a Validation Quantity for this new Custom");

}

else if(document.AddECNumber.CN_Entry_Initials.value == "N" && document.AddECNumber.P_Drive_Docs_Initials.value == "i") { //this will return true if the input passes the regular expression

alert("You cannot select 'i' for docs to be removed for a new part");

}

else if(document.AddECNumber.CN_Entry_Initials.value == ""  && document.AddECNumber.SW_Model_Only.value == "0") { //this will return true if the input passes the regular expression

alert("ECO type required - Select (N)ew or (C)hange for eco line item");

}

else if(document.AddECNumber.PNR_BOM_Change_Only.value == "1" && document.AddECNumber.CN_Entry_Initials.value != "C" && (document.AddECNumber.Release_Status_Initials.value == "U"

|| document.AddECNumber.Release_Status_Initials.value == "N")) { //this will return true if the input passes the regular expression

alert("ECO type required - Select (C)hange for eco line item and Select N for Release Status if PNR/BOM Change Only is yes");

}

else if(document.AddECNumber.PNR_BOM_Change_Only.value == "1" && document.AddECNumber.CN_Entry_Initials.value == "C" && document.AddECNumber.Release_Status_Initials.value == "U") { //this will return true if the input passes the regular expression

alert("ECO type required - Select (C)hange for eco line item and Select N for Release Status if PNR/BOM Change Only is yes");

}

else if(document.AddECNumber.Doc_Changes_Only.value == "1" && document.AddECNumber.CN_Entry_Initials.value != "C") { //this will return true if the input passes the regular expression

alert("ECO type required - Select (C)hange for eco line item since Doc Changes Only is yes");

}

else if(document.AddECNumber.Release_Status_Initials.value == "U"  && document.AddECNumber.SW_Model_Only.value == "1") { //this will return true if the input passes the regular expression

alert("For SW Model only change- Select 'N' for Release Status");

}

<!--- else if (document.AddECNumber.P_Drive_Docs_Initials.value == "i")

// self.location='eco_search.cfm'; --->

  // javascript below is to make sure the correct number value is entered into the Eng_Hrs field. It does not allow letters or more than 2 decimal places. It can also only do 999,999.99, not 1 million.

var validateForm = function () {

                var test = document.getElementsByName("Eng_Hrs");

                var success = true;

                for(var t = 0; t< test.length; t++){

                                var regex  = /^[1-9]\d*(((,\d{3}){1})?(\.\d{0,2})?)$/;

                                if (!regex.test(test.value.replace("$", ""))){

                                alert("You must enter a valid number in the Engineering Hours field");

                                success = false;

                                break;

                                }

                }

                return success;

}

else

{

document.AddECNumber.action ="add_new_ec_number_action.cfm";

document.AddECNumber.submit();

}

 

}

//  End -->

</script>

If you look at the line that starts with var validateForm = function () {    this is the code I'm trying to use. There's other things going on in this javascript, but I thought I would put the whole javascript in here so you could see it all. The cfform name is AddECNumber. The submit button looks like this:

<cfinput type="button" name="submitBtn" onclick="verify()" value="Add & Finish">

I don't need the dollar sign in this anymore. I'm just not sure how to do javascript. Can anyone tell me what I'm doing wrong? Thanks.

Andy

Views

534

Translate

Translate

Report

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
Advocate ,
Jun 05, 2015 Jun 05, 2015

Copy link to clipboard

Copied

My recommendation would be to research jquery and jquery validation. They are very popular and easy to use. It'll do exactly what you are asking and A LOT more.

Votes

Translate

Translate

Report

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
Community Beginner ,
Jun 24, 2015 Jun 24, 2015

Copy link to clipboard

Copied

LATEST

jQuery.isNumeric( value )

so something like:

<html>

<head>

<script src="http://code.jquery.com/jquery-1.7.min.js"></script>

<SCRIPT LANGUAGE="JavaScript">

  /* javascript below is to make sure the correct number value is entered into the Eng_Hrs field.

  It does not allow letters or more than 2 decimal places. It can also only do 999,999.99, not 1 million.

  */

  function validateForm(Eng_Hrs) {

  var success = false;

  var formattedEng_Hrs = Eng_Hrs;

  if ($.isNumeric(Eng_Hrs) && Eng_Hrs < 1000000) {

  var amt = parseFloat(Eng_Hrs);

  formattedEng_Hrs = (amt.toFixed(2));

  //alert(formattedEng_Hrs);

  success = true;

  }

  if (success) {

  alert(formattedEng_Hrs + " is valid.");

  } else {

  alert(formattedEng_Hrs + " is NOT valid.");

  }

  }

</script>

</head>

<body>

<a href="javascript: validateForm('Hello World')">Hello World</a><br/><br/>

<a href="javascript: validateForm('15.416')">15.4162</a><br/><br/>

<a href="javascript: validateForm('999999.99')">999999.99</a><br/><br/>

<a href="javascript: validateForm('1000000')">1000000</a><br/><br/>

</body>

</html>

Votes

Translate

Translate

Report

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
Documentation