0

I want to validate a input fields of form in javascript. I have searched a lot on net and always got different ways to do it. It was so confusing. I want for every single input if it is left empty an alert should popup. Here is my code

    <form method="post" action="form.html" id="FormContact" name="frm">
    <p>Full Name: <br /><br /> <input type="text" name="FullName" size="50" id="Name"></p>  

        <span id="error"></span>

    <p>Email:<br /><br /> <input type="email" name="Email" size="50" id="Mail"></p>

    <p> Subject:<br /><br /> <input type="text" name="subject" size="50" id="Subject"></p>

     Message:<br /><br /> 
     <textarea rows="15" cols="75" name="Comment" id="text">  


    </textarea> <br /><br />

     <input type="submit" value="Post Comment">
</form>

I got it done sometimes but that only worked for Full Name field.

Thanks and regards,

1
  • you should use form validation plugin like jQuery Validator jqueryvalidation.org , its well documented and full of features. Commented Nov 10, 2014 at 4:16

4 Answers 4

1

You can do something like this, to have an alert popup for each empty input.

$('form').on('submit', function(){
   $('input').each(function(){
     if($(this).val() === ""){
       alert($(this).attr('name') + " is empty");
     }
   });
});
Sign up to request clarification or add additional context in comments.

1 Comment

I want javascript's method of doing form validation. I want to validate all fields with javascript please tell me that one thanks
0

http://www.w3schools.com/js/js_form_validation.asp

if you're willing to use javascript, this would be pretty easy to implement.

1 Comment

I also tried this method. but it confused me because it just works for one field which is named as FullName. i dont know how to validate Email and submit with this one Because in this onsubmit is inline and only can be used once. could you please explain little?
0

use jquery validation plugin.

<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<script>

                $("#FormContact").validate({
                    rules: {
                        FullName: {
                            required:true
                        }
                    },
                    messages:{
                        FullName:{
                            required:"Please Enter FullName."
                        }
                    }
                });

            </script>

Comments

0

USE submit method of jquery the use each loop to validate the controls

LIVE CODE

 $('form#FormContact').submit(function(){
    var i= 0;
    $('input').each(function(i,j){
        if($(this).val() == "" || $(this).val() == undefined){
            alert('empty');
            i++;
        }else{
        i=0;
        }
      })  
    if(i == 0){
    return false;
    }else{
          return true;
         }
})

Comments

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.