If you want to simply create the required form element, you must add the required class to the element:
<input type="text" name="name" id="name" class="required" />
This will automatically receive confirmation.
If you are doing this just to figure out how to add a custom rule, I would recommend not using a rule called "name" (I had problems with it in a simple example). Here you can add a custom rule guaranteeing "name" only characters:
$.validator.addMethod("customname", function(value, element) { var i = /^[A-Za-z]+$/; return this.optional(element) || (i.test(value) > 0); }, "Name is required"); $("#sf").validate({ rules: { name: { customname: true } } });
Note that inside the rules object you must specify another object ( name ) that defines the rules for this element.
As for placing the error in a specific place, check the errorPlacement parameter:
errorPlacement: function(error, element) { element.closest("p").prepend(error); }
Puts an error between the label and input .
Here is an example of two actions: http://jsfiddle.net/andrewwhitaker/7xD2H/
source share