javascript - Problems with jquery form validation : not working for one or two empty input fields -
i have 7 input fields in form , using jquery validation plugin validating each field. validation working fine if input fields empty. not working if 1 or 2 input field empty , others have value. want show error message if 1 field empty.
here form:
<form role="form" class="form-inline" enctype="multipart/form-data" method="post" action="insert.php" id="frmform"> <label >audit type</label> <select name="type" > <option value="0">select type </option> <option value="typ1"> type 1</option> <option value="typ2"> type 2</option> </select> <label> site</label> <select name="site" > <option value="0">select site </option> <option value="site1"> site 1</option> <option value="site2"> site 2</option> </select> <label> function</label> <select name="dept" > <option value="0">select function </option> <option value="fn1"> function 1</option> <option value="fn2"> function 2</option> </select> <label> title</label> <input type="text" name="title"/> <label> objective</label> <input type="text" name="obj"/> <label> standard</label> <select name="stand" > <option value="0" >select standard</option> <option value="1" > standard 1 </option> <option value="2" > standard 2</option> </select> <label> remarks</label> <textarea name="remarks"></textarea> </form>
my jquery validation:
<script> $(document).ready(function(){ $("#frmform").validate({ rules: { type: { selectcheck: true }, site: { selectsite: true }, dept: { selectdept: true }, stand: { selectstd: true }, title: { required: true, title: true }, obj: { required: true, obj: true }, remarks: { required: true, remarks: true } } }); }); jquery.validator.addmethod('selectcheck', function (value) { return (value != '0'); }, "this field required "); jquery.validator.addmethod('selectsite', function (value) { return (value != '0'); }, "this field required "); jquery.validator.addmethod('selectdept', function (value) { return (value != '0'); }, "this field required "); jquery.validator.addmethod('selectstd', function (value) { return (value != '0'); }, "this field required "); </script>
please me fix problem..
a simple solution make default <select>
options value=""
, create standard validation:
demo:
https://jsfiddle.net/re3yc0h1/
form: notice have changed dropdowns.
<form role="form" class="form-inline" enctype="multipart/form-data" method="post" action="insert.php" id="frmform"> <label >audit type</label> <select name="type" > <option value="">select type </option> <option value="typ1"> type 1</option> <option value="typ2"> type 2</option> </select> <label> site</label> <select name="site" > <option value="">select site </option> <option value="site1"> site 1</option> <option value="site2"> site 2</option> </select> <label> function</label> <select name="dept" > <option value="">select function </option> <option value="fn1"> function 1</option> <option value="fn2"> function 2</option> </select> <label> title</label> <input type="text" name="title"/> <label> objective</label> <input type="text" name="obj"/> <label> standard</label> <select name="stand" > <option value="" >select standard</option> <option value="1" > standard 1 </option> <option value="2" > standard 2</option> </select> <label> remarks</label> <textarea name="remarks"></textarea> <input type="submit" name="submit" value="submit" /> </form>
jquery validation
<script> $(document).ready(function(){ $("#frmform").validate({ rules: { type: "required", site: "required", dept: "required", title: "required", obj: "required", stand: "required", remarks: "required" }, messages: { type: "you can not leave type empty", site: "you can not leave site empty", dept: "you can not leave dept empty", title: "title required", obj: "you must have object", stand: "standard required", remarks: "please leave remarks" } }); }); </script>
Comments
Post a Comment