jquery too much recursion on multiple blur function
I am checking some textboxes on blur, some need to be checked against the same critera so I put these all in one blur function. I have noticed that my page seems slow and in firebug I keep gettin the error too much recursion
.
My code is below, it checks the textbox and then sends the ID to another function which adds a class to say if it is valid or not valid.
$('#username, #customerName, #customerTown, #customerCounty, #contactName, #staffFirstname, #staffLastname, #staffTown, #staffCounty').blur(function()
{
var ID = $(this).attr('id');
var val = $(this).val();
if(validate(val))
{
valid(ID);
}
else
{
notValid(ID);
}
});
function valid(elementID)
{
$('#'+elementID+'Img').html('<img src="../images/tick.png" alt="Valid" title="Valid" />');
$('#' + elementID).addClass('valid');
}
function notValid(elementID)
{
$('#'+elementID+'Img').html('<img src="../images/cross.png" alt="Not Valid" title="Not Valid" />');
$('#' + elementID).removeClass('valid');
}
function validate(val)
{
var reg = new RegExp ("^([a-zA-Z ]){3,90}$");
var regTest = reg.test(val);
if(regTest)
{
return true;
}
else
{
return false;
}
}
Would it be better if I did a开发者_如何学C seprate blur
function for each textbox? Or is there another way? I don't really want to write a blur function for every single textbox.
Any suggestions? Thanks.
I tried out your code, and it doesn't show any signs of slowness, and there is no error messages at all, not even in the error console.
http://jsfiddle.net/5sFWF/
Here are some suggested changes in the code, using a single function to set the status instead of two, using jQuery to create elements instead of pasting together HTML code, avoiding the if-true-then-true-else-false antipattern in the validate function:
http://jsfiddle.net/5sFWF/2/
精彩评论