Skip to main content
Inspiring
May 12, 2011
Question

Test to see if text in text box starts with a C

  • May 12, 2011
  • 1 reply
  • 2500 views

I have a text box called Part_Number. If the part number starts with a "C", then I want my Validation_Qty text box be a requirement to be filled in. If there's anything else in the Part_Number box, nothing else matters. There are no requirements for any other part numbers. How would I go about dong this? Here are my 2 text boxes:

<input type="text" name="Part_Number" size="30">

<input type="text" name="Validation_Qty" size="12" validate="integer" required="no" message="You must enter a valid number in the Validation Quantity field">

Thanks.

Andy

    This topic has been closed for replies.

    1 reply

    Inspiring
    May 12, 2011

    Assuming that you want to use CFFORM / CFINPUT validation; you can create your own JavaScript to perform your custom validation and include it using the onValidate attribute of CFINPUT.

    Inspiring
    May 12, 2011

    Bob,

       I added this javascript to my page, but this only stops the page from adding if the Validation Qty. box is not filled in at all. It does not worry if the part number box starts with a C or not. How do I do a wildcard in Javascript that says if the Part number starts with a C, then only require that the Validation Qty. be filled in then? I thought this would work: /C^$/  but it doesn't.

    <SCRIPT LANGUAGE="JavaScript">


    function verify() {
    var themessage = "Please enter ";
    if (document.AddItem.Part_Number.value !== /C^$/ && document.AddItem.Validation_Qty.value == "") {
    themessage = themessage + "Validation Qty.";
    }

    if (themessage == "Please enter ") {
    document.AddItem.submit();
    }
    else {
    alert(themessage);
    return false;
       }
      
    }

    </script>

    Andy

    Inspiring
    May 12, 2011

    Here a sample which uses CFINPUT validation.  You might adpat this code for your validation.

    See: http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec22c24-7a7c.html

    <html>
    <head>
         <title>Test Custom JavaScript</title>
    </head>

    <body>


    <cfform id="inputForm" name="inputForm" action="index.cfm" method="post">

    Part Number <input type="text" name="Part_Number" size="30"><br>

    Quantity <cfinput type="text" name="ValidationQty" size="12" validate="integer,range" range="1" required="no" onValidate="validationQtyCustomValidation" message="You must enter a valid number in the Validation Quantity field"><br>

    <input type="submit" value="Submit">

    </cfform>

    <script type="text/javascript">
         function validationQtyCustomValidation(formObject, inputObject, objectValue) {
             
             
              //lookup part number
              var partNum = document.forms["inputForm"]["Part_Number"].value;

              if(partNum.length > 0 && partNum.substring(0,1) == "C")
              {
                   if(objectValue.length < 1)
                   {
                        return false;
                   }
              }

              return true;
         }
    </script>

    </body>

    </html>

    Message was edited by: JR "Bob" Dobbs Added HTML sample.


    Bob,

           This code does not do anything different. It still allows it to go through if a C is typed in or not. This is what I have:

    <script type="text/javascript">
         function validation_QtyCustomValidation(formObject, inputObject, objectValue) {
             
             
              //lookup part number
              var partNum = document.forms["AddItem"]["Part_Number"].value;

              if(partNum.length > 0 && partNum.substring(0,1) == "C")
              {
                   if(objectValue.length < 1)
                   {
                        return false;
                   }
              }

              return true;
         }
    </script>

    <input type="text" name="Part_Number" size="30">

    <input type="text" name="Validation_Qty" size="12" validate="integer,range" range="1" onValidate="validationQtyCustomValidation" message="You must enter a valid number in the Validation Quantity field">

          I had to remove the required="no" in the text box for the Validation_Qty field because it does not go through at all if nothing is entered into this box. Why doesn't this script stop if a C is in the box?

    Andy