Custom jQuery validation

HTML
<style>
   .redstar{color:red;}
   .error {color: red;}
   .hidden {display: none;}
</style> 
<div class="col-md-4">
     Department<span class="redstar">*</span>
     <input type="text" placeholder="" class="form-control" name="department_name" id="department_name">
     <span id="msgdepartment_name" class="error hidden">this field is required</span>
 </div>
 <div class="col-md-4">
     Course title<span class="redstar">*</span>
     <input type="text" placeholder="" class="form-control" name="course_title" id="course_title">
     <span id="msgcourse_title" class="error hidden">this field is required</span>
 </div>
Validate function
function validateFields() {
        var flag = false;
        var department_name = $('#department_name').val();
        if (department_name == "") {
            $('#msgdepartment_name').removeClass('hidden');
            flag = false
        }
        else {
            flag = true;
            $('#msgdepartment_name').addClass('hidden');
        }
        var course_title = $('#course_title').val();
        if (course_title == "") {
            $('#msgcourse_title').removeClass('hidden');
            flag = false
        }
        else {
            flag = true;
            $('#msgcourse_title').addClass('hidden');
        }
    
       
        if (department_name != '' && course_title != '' && flag == true)
            return true;
        else
            return false;
    }

Post a Comment

0 Comments