Let's say I have a text field:
Username: <input type="text" id="username" class="required" />
<input type="submit" />
and I have some jQuery:
$(document).ready(function() {
user = $("#username");
user.submit(function() {
})
})
I want to check if the length of the value entered in username
is between 2 - 5 characters after I submit. If it's not then something happens.
I'm not too sure what I should be inside the user.submit(function(){})
.
First you'll need an actual form:
then:
Or in HTML5 you can use the
pattern
attribute (not supported in Safari and IE9-and below):FIDDLE
You can check the value of the input field with
user.val()
and the length of the string withuser.val().length
.That way you can do something like this:
Bind submit to
form
instead of input type text and check its value in submit event. It would be better to assign some id to form and use form id selector to bind submit.I made a Fiddle
You can use a statement as
And HTML