This is my first time creating a plugin. I am trying to make a plugin that will validate whether an input field is empty or not.
My problem is with the keyup()
function - if I call it as I did in 1.php
it works fine, but I want to put keyup()
in a plugin as I did in vall.js
, it is commented out as $(this).keyup(function()
is not working. Should I place keyup()
somewhere else or make a separate keyup()
function.?
Later I want to add keyup()
, focus()
... to options so I can just write keyup:true
or something like that.
val.js
( function($){
$.fn.validation = function(options) {
var settings= {required: '',rqrmsg:''};
if(options){$.extend(settings, options); }
var options = $.extend(settings, options);
// $(this).keyup(function() {
if(options.required == 'true') {
var error = 0;
var elem1 = $(this).val();
if (elem1 == '') {
error = 1;
this.css({'border':'Solid 1px Yellow','background-color':'red'})
.next().fadeIn('fast').text(options.rqrmsg)
} else {
error;
$(this).css({'border':'Solid 1px Yellow','background-color':'green'})
.next().fadeOut();
}
error;
}
//});
};
})(jQuery);
This is where I am calling the main jQuery file:
1.php
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="val.js"></script>
<form id='targets' name='targets' action='' method='post'>
<span class="">Email</span>
<input type='text' name='test' id='email' /><span class="error"></span><br />
<input type='text' name='test1' id='miho' /><span class="error"></span><br />
<input type='submit' value='Save' id='save'/>
</form>
<script>
$(document).ready(function() {
$('#email').keyup(function() {
$('#email').validation({required:'true',rqrmsg:'sdkgjfikshrik'});
});
$('#miho').keyup(function() {
$(this).validation({required:'true',rqrmsg:'miso'});
});
});
</script>