1

I have jQuery validation for my input fields. But seems like not working :(

$('form')
.submit(function() {

    var name = $.trim($('#TxtName').val());
    var dob = $.trim($('#TxtDesig').val());
    var salary = $.trim($('#TxtSalary').val());

    if (name === "" || dob === "" || salary ==="") {
        alert("All fields are mandatory");
    }
    return false;
});

Here is my html form:

<form class="form-group" method="post">
        <label class="control-label">Employee Name:</label> 
            <input class="form-control" type="text" id="TxtName" name="EmployeeName" value="" /><br />
        <label class="control-label">Designation:</label> 
            <input class="form-control" type="text" id="TxtDesig" name="Designation" value="" /><br />
       <label class="control-label">Salary:</label>
            <input class="form-control" type="date" id="TxtSalary" name="Salary" value=""/><br/>
4
  • 1
    "Not working" is not a useful statement. What happens? Console errors? Commented Nov 7, 2016 at 14:01
  • use event.preventDefault(); in your .submit function Commented Nov 7, 2016 at 14:01
  • 1
    You likely want to change $('form').submit(function() { to $('form').submit(function(e) { and add e.preventDefault() when in error Commented Nov 7, 2016 at 14:01
  • @mplungjan I mean no validation messages are appearing even the fields are empty Commented Nov 7, 2016 at 14:05

3 Answers 3

1

Here is how to code your jQuery validation.

I need to show it in a fiddle since the stacksnippet does not allow form submit

https://jsfiddle.net/mplungjan/n6mcyf6x/

$(function() {
  $('form').on("submit", function(e) {
    var name = $.trim($('#TxtName').val());
    var dob = $.trim($('#TxtDesig').val());
    var salary = $.trim($('#TxtSalary').val());
    if (name === "" || dob === "" || salary === "") {
      alert("All fields are mandatory");
      e.preventDefault();
    }
  });
});

As mentioned by Rhys Bradbury an alternative is to add "required" to each field .
It may however not be supported by older browsers (like IE<10) for example)

http://caniuse.com/#feat=form-validation

Sign up to request clarification or add additional context in comments.

6 Comments

Unfortunately, this is not working too. I will give a try using "required" attribute but not sure whether I am allowed to do that because I have very little idea about what browsers our client end users are using.
Yes this is working. Click the jsfiddle to see it work.
oops, don't know why not working in my page :( I'm using the following imports, are they fine? <script src="~/Scripts/jquery-3.1.1.min.js"></script> <script src="~/Scripts/jquery.validate.min.js"></script> <script src="~/Scripts/Employee/CreateNewEmployeeFormValidation.js"></script>
Without seeing your page I do not know either. Did you load jQuery? Any console messages?
no error message, I think some issue with the imports. jquery.min.js, jquery.validate.min.js and my custom js file, I'm importing these 3 js files to my page. Is that ok?
|
1

Why do this in jQuery? Why not use HTML required attribute on form inputs?

ref: http://www.w3schools.com/jsref/prop_text_required.asp

example:

<input placeholder="required a value here please" required/>

FYI this is HTML5

1 Comment

Yeah. Why JQuery? HTML required is easier.
1

You can validate by two process.

process 1: Add following attribute into validated controls as one I edited from your source. Easiest way rest will be responsible of jQuery validation engine.

 <input class="form-control" type="text" id="TxtName" name="EmployeeName" value="" required='' data-msg-required='Please provide name.'/>

Process 2: If you want your controlled validation then you need write piece of code as follows.

$("#form").validate({
    rules:{   //Validation rules work on name attribute
         EmployeeName:{  //Condition Area
               required:true
         }
    },
   messages:{   //user information area
       EmployeeName:
       {
           required:"Please provide Employee Name"  /// Message to user for required fields
       }
   }

}); 

You get more information from jQuery Website

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.