3 Replies Latest reply on Apr 23, 2016 2:54 PM by Al_NZ

    validating input but clear button requires you to allow "" value - help

    Al_NZ Level 1

      Hello,

       

      I have a form with a validate script below which is a required field for a time. The regex stuff validates and formats times from 0100 to 01:00 etc . I want it so that before the user can submit the form it must have a valid time in this field.

       

      The form also has a "reset" or clear button - in order to allow this button to function the validate script for the time field needs to return event.rc = true for value "" - but then this defeats the purpose of forcing a user to have a value in this field before the form is submitted?

       

      How do I get around this?

       

      Cheers

       

      -Al

       

       

      event.rc = true;
      var ltimeRegExp = /^((?:[0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9])$/;
      var stimeRegExp = /^((?:[0-9]|0[0-9]|1[0-9]|2[0-3]):?[0-5][0-9])$/;
      var strTime = String(event.value);
      
      if (ltimeRegExp.test(strTime) != true && strtimeRegExp.test(strTIme) !=true {
           app.alert("Does not appear to be a valid time format");
           event.rc = false
      } else if (ltimeRegExp.test(strTime) == true) {     
           // no need to do anything as time is valid
           event.rc = true
      } else if (strTime.length == 3) {
           newstrTime = "0" + strTime.slice(0,1) + ":" +strTime.slice(1,3);
           event.value = newstrTime;
           event.rc = true;
      } else if (strTIme.length == 4) {
           newstrTime = strTime.slice(0,2) + ":" +strTime.slice(2,4);
           event.value = newstrTime;
           event.rc = true;
      } else if (strTime == "") {
           event.rc = true;
      } else {
           app.alert("no subroutine entered");
      }
      
      
      
        • 1. Re: validating input but clear button requires you to allow "" value - help
          George_Johnson MVP & Adobe Community Professional

          There are some errors in that code that would prevent it from working, so I'll assume they're just typos.

           

          If you mark the field as required, it won't be allowed to be submitted (via email or to a web server) unless the field has a non-blank value, and your validation script ensures a valid entry if there is one, so this shouldn't be a problem. If you don't want to mark it as required you can use a script to check the field and only allow the submit to take place if it's not blank.

          • 2. Re: validating input but clear button requires you to allow "" value - help
            try67 MVP & Adobe Community Professional

            If the issue is that this code rejects blank values, change it like this:

             

            event.rc = true;

            if (event.value) {

                 // put rest of code here

            }

            • 3. Re: validating input but clear button requires you to allow "" value - help
              Al_NZ Level 1

              Hi George,

               

              Yes thanks for that - the submit button is via email and makes the check that the required fields are not blank.

               

              The validation routine allows any expression that matches the regex or ""

               

              This also allows the clear button to work without triggering a validation error.

               

              My final code was this (I cant seem to cut n paste in here?)

               

              event.rc = true;
              var ltimeRegExp = /^((?:[0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9])$/;
              var stimeRegExp = /^((?:[0-9]|0[0-9]|1[0-9]|2[0-3]):?[0-5][0-9])$/;
              var strTime = String(event.value);
              
              if (strTime=="") {
                   event.rc=true;
              } else if (ltimeRegExp.test(strTime) != true && stimeRegExp.test(strTime) != true) {
                   app.alert("Does not appear to be valid time format \n Must be 3-4 digits in length");
                   event.rc = false;
              } else if (ltimeRegExp.test(strTime) == true) {
                   //app.alert("Time is valid - need to pass time back exit unchanged");
                   event.rc = true;
              } else if (strTime.length == 3) {
                   newstrTime = "0" + strTime.slice(0,1) + ":" + strTime.slice(1,3);
                   event.value = newstrTime;
                   event.rc = true;
              } else if (strTime.length == 4) {
                   //app.alert(strTime);
                   newstrTime = strTime.slice(0,2) + ":" + strTime.slice(2,4);
                   event.value = newstrTime
                   event.rc = true;
              } else if (typeof(strTime) == 'undefined') {
                   getField(fieldname).setFocus();
                   getField(fieldname).value = "";
              } else if (strTime=="") {
                   event.rc=true;
              }
              

               

              This seems to work. Open to suggestions, but I think I catch most exceptions.

               

              I was also thinking about making this a function since I have 4 fields to run this code on, and just call the function time(timefieldname, timefieldvalue) and return true or false.

               

              Somehow the odd user still emails a form with some of these fields bank.

               

              Cheers

               

              -Al