i m currently reading jquery and i m facing some issues on jquery functions.. can any one guide me regarding functions . currently i want to create only one function and call twice a time if req is same .. but function is not working pls experts guide me in the code .
var errorAlpha = function(){
$(this).focus().keyup(function(){
var reg = /^([A-Za-z]+)$/;
var check = $(this).val();
if(reg.test(check)==true && check.match(reg)==true)
{
$(this).removeClass('error_Aplha');
}
else {
$(this).addClass('error_Aplha');
}
}),blur(function(){
var check = $(this).val();
var reg = /^([A-Za-z]+)$/;
if(reg.test(check)==true && check.match(reg)==true)
{
$(this).removeClass('error_Aplha');
}
else {
$(this).addClass('error_Aplha');
}
});
};
$('#step1 #fName').function(errorAlpha());
Html Is here
<input type="text" class="width-260" id="fName"/>
Css is here
.error_Aplha {
border:1px solid #b20000 !important;
box-shadow: 0px 0px 5px #b20000;
-webkit-box-shadow: 0px 0px 5px #b20000;
-moz-box-shadow: 0px 0px 5px #b20000;
}
Your code
$('#step1 #fName').function(errorAlpha());is wrong. Nothing is going to happen.If you want to trigger something then you need to attach a event handlers like click, key, mouse events. Here is the one way to set event handler,
Here is the list of jQuery event methods, http://api.jquery.com/category/Events/
Update:
I updated your code and it’s working here http://jsfiddle.net/muthkum/gU3PU/2/