I am trying to write a script that validates user input. Like: letter length more than 5 or password and reentered-password match. But how can I get which input box in the form was clicked? I used
$(function() {
$('#register input').keyup(function() {
if ($('#username').val().length < 5) {
$('a.username').text("Please fill");
} else {
$('a.username').text("");
}
if ($('#password').val().length < 5) {
$('a.password').text("asdasd");
} else {
$('a.password').text("");
}
});
});
And
New User User Name
<label for="password">Password</label> <input type="password" id="password" name="password"><a class="password"></a>
</fieldset>
</form>
When I click one of input elements, the code runs and it sees that second password input is also not full field and second <a>
tag also fires. Should I use several .keyup
functions for each element, or is there any method for getting which element clicked? Like
if (clicked == $("input#username") && $('#username').val().length < 5) {
$('a.username').text("Please fill");
} else {
$('a.username').text("Please fill");
}
but this does not work. I think there should be something similar.