0

I am using javascript validations for required field. Here is my html

<form class="uk-form-stacked" name="myForm"  action="<?php echo base_url(); ?>admin/pages/create_service"  id="wizard_advanced_form" method="post" enctype="multipart/form-data" onsubmit="return myFunction(this)" novalidate>
                    <div data-uk-grid-margin="" class="uk-grid">
                    <div class="uk-width-medium-1-2">
                        <label for="service_title">Service Title<span class="req">*</span></label>
                        <input type="text"  name="service_title" id="validd"  class="md-input"  />
                        <p id="demo"></p>
                    </div>
                </div>
                    <div class="uk-grid">
                    <button type="submit" class="md-btn md-btn-primary md-btn-wave-light waves-effect waves-button waves-light" >Submit</button>
                </div>
            </form>

my javascript is

    <script>
    function myFunction(form) {
   var x, text;
   x = document.getElementById("validd").value;
   if (x == null || x == "") {
    text = "Input not valid";

   }
     document.getElementById("demo").innerHTML = text;
     return false;

     }
     </script>

Now when my input field is empty and i submit form it shows me input not valid that is fine. but even when i fill some textin input then it shows me undefined in place of input not valid instead of submitting form. please help..

1
  • Reason is quiet simple, variable is defined as undefined, if condition is not satisfied then value remains undefined.. Commented Aug 19, 2016 at 5:21

2 Answers 2

2

You forgot to add an else where it would return true if the condition is not satisfied.

 <script>
        function myFunction(form) {
       var x, text;
       x = document.getElementById("validd").value;
       if (x == null || x == "") {
        text = "Input not valid";
        document.getElementById("demo").innerHTML = text;
         return false;
       }
        else{
          return true;
       } 
         }
 </script>
Sign up to request clarification or add additional context in comments.

Comments

0

You can use instead:

    <html>
      <head>
        <script>
        function valid()
        {
           var x;
           x=document.getElementById(validd).value;
           if(x==null || x=="")
           {
              alert("Please input service title");
              document.getElementById(validd).focus();
              return false;
           }  
        }
      </script>
      </head>
      <body>
        <form name="myForm" onsubmit="return valid()">
          <input type="text" name="service_title" id="validd"/>
          <button type="submit">Submit</button>
        </form>
      </body>
    </html>`

2 Comments

Please add more detail on how this helps.
if we can use alert function instead of set text to textfield so it doesnt make any problem because alert function gives popup msg and after we say ok it close so it is better for form validation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.