javascript - Two fields validation -
<html> <head> </head> <body> <form class="form-horizontal cmxform" id="validateform" method="get" action="../../course_controller" onsubmit="return validate();" autocomplete="off"> <input type="text" id="course_name" name="course_name" placeholder="enter course name..." class="row-fluid" required onkeyup="javascript:return validate_course_name();"> <label id="course_name_info" style="color:rgba(255,255,255,0.6);font-size:13px"> </label> <input type="text" id="course_desc" name="course_desc" placeholder="enter course name..." class="row-fluid" required onkeyup="javascript:return validate_course_desc();"> <label id="course_desc_info" style="color:rgba(255,255,255,0.6);font-size:13px"> </label> <button type="submit" name="user_action" value="add" class="btn btn-primary" >save</button> <button type="reset" class="btn btn-secondary">cancel</button> </form> <script type="text/javascript"> /**** specific js page ****/ //validation things function validate_course_name(){ var tcode = document.getelementbyid('course_name').value; if( /[^a-za-z1-9 _-]/.test( tcode ) ) { document.getelementbyid('course_name_info').innerhtml="please enter alphanumeric or _,-,' ' "; return false; } else { document.getelementbyid('course_name_info').innerhtml="please enter alphanumeric or _,-,' ' "; return true; } } function validate_course_desc(){ var tcode = document.getelementbyid('course_desc').value; if( /[^a-za-z1-9 _-]/.test( tcode ) ) { document.getelementbyid('course_desc_info').innerhtml="please enter alphanumeric or _,-,' ' "; return false; } else { document.getelementbyid('course_desc_info').innerhtml="please enter alphanumeric or _,-,' ' "; return true; } } function validate(){ return validate_course_name(); return validate_course_desc(); } </script> </body> </html>
so code ...i applying alpha numeric validation on 2 field problem if give first input field valid input , second invalid form submitted doing wrong? ...i new web appreciated:)
updated answer:
fine! different =)
one line, should validate both fields regardless if validate_course_name()
returns false
.
jsfiddle:
http://jsfiddle.net/fvqty/3/
function validate() { return (validate_course_name() * validate_course_desc()) == true; }
let false
= 0, true
= 1. math :)
Comments
Post a Comment