I have same input class,type in several pages like the following :
<input type="text" name="studentID" id="studentID" class="form-control student-id"/>
what I want using the same class name student-id
,
it will validate student id using the following js :
function validateStudentId(){
var studentId= document.getElementsByClassName('student-id');
if (studentId.length > 0) {
var id = studentId[0].value;
console.log('lengthe'+id.length);
if(id.length > 7){
alert('Please enter valid student id .');
$('.student-id').val("");
return;
}
if(isNaN(id)){
alert('Entered input is not a number .');
$('.student-id').val("");
return;
}
}
}
To do this job I've already done the following :
<input type="text" class="form-control student-id" onchange="validateStudentId()" name="studentid" size="10" maxlength="7" />
An onchange function added. Is there any better way to do this.
coz I have to do this onchange
function call every time.
So what I want is to give only class name and it will automatically validate the field using the class name.
Suggest me any better idea, Just dont want to write onchange function every time ?? Thanks