PDA

View Full Version : javascript character validation



mutago
10-03-2014, 08:43 PM
This script ensures that if the character length is not equal to 6. it will returns error message.




<script type="text/javascript">
function validate(){
x=document.myForm
input=x.myInput.value


//if (input.length != 6 OR 10){


if (input.length != 6){


alert("The field must contain 6 character or 10 character!")
return false
}else {
return true
}
}
</script>






Now I want to return error message if character length is not equal to 6 or 10. how do i proceed

Thanks

Deadweight
10-03-2014, 10:32 PM
if (input.length >= 6 && input.length<=10){
return true
}else {
alert("The field must contain 6 character or 10 character!")
return false;
}


Meaning of code:
if the input length is between 6 and 10 both inclusive then do this.
else if it is less than 6 (not including 6) or greater than 10 (not including 10) do this.

Or if you want something like this


if (input.length >= 6 && input.length<=10){
return true
}else if(input.length < 6){
alert("The field is too short must contain at least 6 character!")
return false;
}else{
alert("The field is too long must contain no more than 10 character!")
return false;
}