How can i use jQuery to validate if a textbox is not empty and consists of digits, when i click on a button?
This is the code i have so far:
HTML:
<input type="button" name="Tilføj" value="Tilføj" id="add" />
<input type="text" id="element" name="element" />
<div id="result"></div>
Javascript:
$(document).ready(function () {
$("#add").click(function () {
$("#element").validate({
rules: {
element: {
required: true,
number: true
}
},
errorHandler: function () {
$("#result").append("error");
},
submitHandler: function () {
$("#result").append("good");
}
});
});
});
Do i have to use jQuery validation inside a form for it to work?
You can do this without a jQuery plugin. All you need is a simple Regular expression to validate your input format. You can customize it for different type of inputs