jQuery validate - group inputs with similar rules - jquery-validate

I am using jquery validate, to validate a submitted form. The form has three input fields which have same rule i.e., only number and required - true. Can i write a single rule for all the three inputs say -
rules: {
"depository.startDate":{
required:true,
digits:true
},
"depository.endDate":{
required:true,
digits:true
},
"depository.port":{
required:true,
digits:true
}
},
to
rules: {
"depository.startDate, depository.port, depository.endDate ":{
required:true,
digits:true
}
},
I tried but its not working, any other way i can get this validation work.
--
Thanks

Use the built-in rules() method to add rules. See documentation.
Note: You must call this method after you call .validate(), and it must be combined with an .each().
jsFiddle DEMO
HTML:
<input type="text" name="depository.startDate" />
<input type="text" name="depository.endDate" />
<input type="text" name="depository.port" />
jQuery:
$('#form').validate({
// your other options
});
// the following method must come AFTER .validate()
$("input[name*='depository']").each(function() {
$(this).rules('add', {
required: true,
digits: true
});
});
This method can also be very useful when you are dynamically adding fields to your form.
The following to combine with custom messages:. Note that the format is slightly different than when adding rules as options within .validate()...
$("input[name*='depository']").each(function() {
$(this).rules('add', {
required: true,
digits: true,
messages: {
required: "Required input",
digits: "Only digits please"
}
});
});
Alternatively, you can use a class instead, but for it to work properly, you must also combine it with an .each()...
jsFiddle DEMO
HTML:
<input type="text" class="myclass" name="depository.startDate" />
<input type="text" class="myclass" name="depository.endDate" />
<input type="text" class="myclass" name="depository.port" />
jQuery:
$('form').validate({
// your other options
});
// the following method must come AFTER .validate()
$('form').find('.myclass').each(function() {
$(this).rules('add', {
required: true,
digits: true
});
});

Related

ko async validation plugin

[UPDATED]
I am not sure how to add a custom (html) message to my async knockout validator.
Below is a simple form. You can see that there is already some isModified and isValid validation in-place, but it is not async. (I am not sure if this complicates or simplifies the solution.)
<form
method="post"
action="Registration()"
novalidate
data-bind="submit: onRegister">
<div
class="input-wrapper email"
data-bind="
validationOptions: {
messageTemplate: 'emailCollisionTemplate'
},
validationElement: EmailAddress,
css: { 'validation-success': EmailAddress.isModified() && EmailAddress.isValid() }">
<input
type="email"
id="registerModalEmail"
data-bind="
value: EmailAddress,
ariaInvalid: EmailAddress"
aria-describedby="email-address-error"
required />
</div>
<div
class="validation-error-message"
id="email-address-error"
data-bind="validationMessage: EmailAddress">
</div>
</form>
<script type="text/html" id="emailCollisionTemplate">
<span>This email is already registered. Please sign in.
</span>
</script>
And then some js:
self.EmailAddress = ko.observable().extend({
required: {
params: true,
message: 'required'
)
},
pattern: {
params: '#',
message: 'match'
)
},
validation: {
async: true,
validator: function (val, parms, callback) {
$.ajax({
url: 'myUrl/isEmailAllowed',
data: { 'EmailAddress': val },
type: 'POST',
success: function (results) {
if (results) {
callback(true);
}
},
error: function (xhr) {
callback(false);
}
});
}
}
});
(isModified and isValid is handled elsewhere, so I have not included it.)
How do I configure the messageTemplate to show? I've looked at examples, but none of them seem to match what I'm doing here. I've put the validationOptions into the data-bind like it seems to say, but that's not working.
I should point out two confounding aspects here:
There are other validators on this same field (i.e.required and pattern)
There are other fields in this form with validation. (The configuration object says the messageTemplate should be 'id of the script that you want to use for all your validation messages'. I only want it for this one field.)

How to jQuery validate custom radio buttons

The form
<form id="form_reg" name="form_reg" method="post" action="">
<input type="radio" id="y" name="opt" value="y"><label for="y"><span>YES</span></label>
<input type="radio" id="n" name="opt" value="n"><label for="n"><span>NO</span></label>
<button id="reg_submit" value=" " type="submit"></button>
</form>
JS Validation
$().ready(function() {
$("#form_reg").validate({
rules: {
opt:{
required: true
}
},
messages: {
opt: {
required: "Select one "
}
}
});
$("#form_reg").validate();
})
This works good until I add CSS
input[type="radio"] {
display:none;
}
input[type="radio"] + label span {
width:27px;
height:25px;
background:url(/images/radio.svgz) no-repeat;
cursor:pointer;
}
input[type="radio"]:checked + label span {
width:27px;
height:25px;
background:url(/images/radio_a.svgz) no-repeat;
cursor:pointer;
}
I guess this is because the default radio buttons are hidden.
How can I validate this form with these custom radio inputs?
To perform a validation on hidden fields you need to override the default 'ignore' setting with an empty array
$(function() {
$("#form_reg").validate({
ignore:[],
rules: {
opt:{
required: true
}
},
messages: {
opt: {
required: "Select one "
}
}
});
});
Presumably you have a good reason to want to require a radio button that the user can't see. For instance when validating a form that is spread across several jquery ui tabs for instance, some fields may be hidden when the form is submitted.
Don't call validate twice on document ready also - although it does no harm there is no point the second call won't do much.

jQuery validate plugin: use different multilingual error message on each required input box

I am using jQuery validate plugin, suppose I have 3 input box and they are required fields.
<input type="text" name="firstname" class="required" />
<input type="text" name="lastname" class="required" />
<input type="text" name="email" class="required" />
I want to have different error message, so I configure as below:
$("#myForm").validate({
messages: {
firstname: {
required: 'Please enter firs tname'
},
lastname: {
required: 'Please enter last name'
},
email: {
required: 'Please enter email'
}
},
submitHandler: function(form) {
form.submit();
}
});
However, now I want to have different multilingual error message on each required input box, so I tried to configure as below, the result is not work.
$("#myForm").validate({
messages: {
firstname: {
required: window.lang.currentLang == 'en' ? 'Please enter email' : '請輸入電子郵件地址'
}
},
submitHandler: function(form) {
form.submit();
}
});
Could someone advise the way to configure the plugin to have different multilingual error message.
Thanks

Password confirmation not working - jquery validation plugin

I can't get why the same code isn't working. I'm using devise gem, so all input names are the same.
My password confirmation didn't work in other project.
Here is js code:
$('.sign_up').validate({
rules: {
'user[password]': {
required: true,
minlength: 6
},
'user[password_confirmation]': {
required: true,
minlength: 6,
equalTo: "#user_password"
}
}
My html code:
<label for="user_password">Password</label>
<input id="user_password" type="password" size="30" name="user[password]">
<label for="user_password_confirmation">Re-Type Password</label>
<input id="user_password_confirmation" type="password" size="30" name="user[password_confirmation]">
Where is problem ?
Try don't use ''
$('.sign_up').validate({
rules: {
user[password]: {
required: true,
minlength: 6
},
user[password_confirmation]: {
required: true,
minlength: 6,
equalTo: "#user_password"
}
}
and use the same word for input's id and name.

jquery.validation required not stopping postback

I am using jQuery validation to attempt to keep a user from posting back unless he meets a couple of qualifications. But the required option doesn't seem to be keeping the form from submitting...
Here is jsFiddle with an example: jsFiddle
I double checked my jQuery code and it would seem as though it was working. jsFiddle says it valid code. So I am not sure what the hangup is?
Thanks
jQuery.validate uses names for the rules, not ids. So replace the ids with names in your input fields.
Also your form should be <form id="temp1"> and not <form id="#temp">.
Here's a link to your fixed code:
<form id="temp1">
<input type="text" name="HospitalFinNumber"/> Hospital Fin<br/>
<input type="text" name="DateOfBirth"/>Date of Birth<br/>
<input type="text" name="AdmitDate"/>Admit Date<br/>
<input type="text" name="Comment"/>Comment<br/>
<input type="submit" value="Save" class="button" id="btClick"/>
</form>
and the script:
$(document).ready(function () {
$("#temp1").validate({
rules: {
HospitalFinNumber: {
required: true,
minlength: 6
},
DateOfBirth: {
required: true
},
AdmitDate: {
required: true
},
Comment: {
required: function (element) {
return $(element).val().length < 4000;
},
maxlength: 4000
}
},
messages: {
HospitalFinNumber: 'Please Enter a Hospital Fin number',
DateOfBirth: 'Please enter a valid Date Of Birth',
AdmitDate: 'Please select an Admit Date',
Comment: 'Why dont you stop attempting to put in more than 4000 characters? Thanks...'
}
});
});

Resources