i am validating the text box value on following conditions
-
It should be minimum of 3 characters
-
It should contain only alphabets
-
It should contain at least one vowel
If all the conditions get matches I am enabling the submit button.
If any of the above case are not matched I am setting an error message to div named error.
Here is my code
$document.ready(function(){
$('.tbox').focus(); // cursor in text box
$(':submit').attr("disabled",true); // disable generate button
$('.tbox').focus(fucntion(){
check_the_input()
});
});
function check_the_input()
{
var value = $(this).val();
$('.tbox').bind('keyup blur',function(){
$(this).val( $(this).val().replace(/[^a-z]/g,'') ); }
if(value.length > 3)
{
if(value.indexOf('a') == -1 && value.indexOf('e') == -1 && value.indexOf('i') == -1 && value.indexOf('o') == -1 && value.indexOf('u') == -1)
{
$('#error').text("*Input must contain atleast one vowel");
$(':submit').attr("disabled",true);
}
else
{
$(':submit').attr("disabled",false);
}
}
else
{
$('#error').text("*Input should be atleast 3 characters")
$(':submit').attr("disabled",true);
}
setTimeout(check_the_input,100);
}
I am facing following issues here:
-
If I type input as aa99 the input changes to aa , but still the generated button is enabled.
-
I am not getting the error at the time I type the text. Only after pressing tab or clicking mouse outside the textbox I am getting error.
I would suggest rethinking the approach. Consider this simpler example:
HTML:
jQuery:
By using a function
testand an object to store your validation filters your code becomes more obvious, more elegant and you get better performance and it’s easier to maintain for example to add new filters or more inputs.Demo: http://jsbin.com/ezatap/6/edit