I have a text box that is getting autocompleted when the person starts typing in the project number. This works fine but now I wanted to validate that if they just type in a number a开发者_如何转开发nd dont select one from the list that it validates that it is a job. Here is the code I have for the autocomplete and the text box's id is projectnum.
$( '[name="projectnum"]' ).autocomplete({
source: "job_validate.php",
minLength: 3
});
You can use the remote attribute. This will check on submit of the form and onblur of the textbox to verify the job is valid.
http://docs.jquery.com/Plugins/Validation/Methods/remote
$("#yourForm").validate({
rules: {
projectnum: {
required: true,
remote: "job_validate.php"
}
}
});
If you are using the jQuery UI autocomplete you could use something like the combobox they have on the demo page. It uses the autocomplete functionality but you are only able to select a value in the list.
http://jqueryui.com/demos/autocomplete/#combobox
This uses a source local to the page rather than ajax calling to the server.
This won't allow nothing but numbers, left, right:
$("#field_id").keydown(function (event) {
if ((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105)) // 0-9 or numpad 0-9
{
// check textbox value now and tab over if necessary
}
else if (event.keyCode != 8 && event.keyCode != 46 && event.keyCode != 37 && event.keyCode != 39) // not esc, del, left or right
{
event.preventDefault();
}
// else the key should be handled normally
});
Taken from http://snipt.net/GerryEng/jquery-making-textfield-only-accept-numeric-values
精彩评论