jquery validation only digits - javascript

I am having the jquery.validate.js plugin and it is working fine for me.
My question is :
I am having a textbox and this textbox is mandatory and should only accept digits.
In the js, I can see that there is validation for the digits and the problem is that I do not know how to use it.
I only knew how to make the field required by putting in the textbox field <class="required">
So, how can I add one more validation criteria to accept only digits.
Thanx

to check it add
$("#myform").validate({
rules: {
amount: {
required: true,
digits: true
}
}
});
http://docs.jquery.com/Plugins/Validation/Methods/digits

$("#mobile_number").validate({
rules: {
mobile_number: {required: true,number:true}
},
messages: {
mobile_number: {required: "Please Enter Your Mobile Number",number:"Please enter numbers Only"}
}
});

Related

Jquery vaildation plugin multiple error in one field using localization

I'm using jquery validation plugin and need to use localization for different language.
The original code would be like this
$('#form').validate({
rules: {
'checkbox1[]': { required: true },
'email': { required: true,
email: true
},
},
messages: {
'checkbox1[]': "Please choose at least 3",
email: {
required:'Please input email',
email: 'Please input valid email'
}
}
});
If I use localization, my message_en.js would be:
$.extend($.validator.messages, {
required: "Please choose your answer.",
email: "Please input valid email.",
});
However, I shared the "required" message in email field. It means that if I didnt input email, the error message would become "Please choose your answer.". Obviously, it is not appropriate.
So, how can I separate the error message?

Validating generated number input with Bootstrap FormValidation

I've been struggling to get this field validation to work. I'm using the JS validation for bootstrap from http://formvalidation.io/ and I've examined http://formvalidation.io/settings/ but nothing I've tried is working.
The field needs to validate a number input on a dynamically generated field using Razer C#, and the form is built to submit multiple models, therefore my name attribute is values[#i].Rating for each element that is generated, where i is an integer that is incremented in a loop.
The validation must make sure the client enters a number between 1 and 4 (inclusive), and if it is not a number between 1 and 4, it must show an error message such as "Please enter a number between 1 and 4". Here is my code, I first tried it with HTML attributes:
<input style="text-align: center" type="number" data-fv-between-min="1" data-fv-between-max="4" data-fv-between-inclusive="true" data-fv-between-message="Please enter a rating between 1 and 4" name="values[#i].Rating" class="form-control" />
but this didn't work, so I tried the javascript route with:
$(document).ready(function () {
$('#newTSRForm').formValidation({
framework: 'bootstrap',
fields: {
selector: '[type="number"]',
input: {
validators: {
between: {
min: 1,
max: 4,
message: 'Please enter a value between 1 and 4'
}
}
}
}
})
});
but my syntax is probably wrong or my thinking is incorrect. I also tried
$(document).ready(function () {
$('#newTSRForm').formValidation({
framework: 'bootstrap',
fields: {
'values[]': {
validators: {
between: {
min: 1,
max: 4,
message: 'Please enter a value between 1 and 4'
}
}
}
}
})
});
But this doesn't work either. I have made sure my set up is correct so the problem is simply syntax or plausability. Can anybody advise?
¿ Did you look at http://formvalidation.io/examples/validating-field-special-name/ ?
I think you have to add every single field with the AddField method of the Plugin.. Adding dynamic field

How to make jquery validates if the two inputs are the same [duplicate]

This question already has an answer here:
jQuery Validation Plugin - equalTo not working
(1 answer)
Closed 8 years ago.
I want to do is when a user type an email in the email input form it should be the same email in the reemail input form. How do i do it in jquery validation?
current output: http://jsfiddle.net/5kcsn/75/
Use equalTo
TRY THIS
email: {
required: true,
email: true
},
reemail: {
required: true,
email: true,
equalTo: "#email"
},
DEMO

Custom error messages for Groups within the jQuery Validation plugin

I'm using the jQuery Validation plugin and i've started to group some of my fields together:
groups: {
fullName: "myFirstName myLastName"
},
I've also added the fields to the rules section so that they are validated:
rules: {
myFirstName: {
required: true
},
myLastName: {
required: true
}
},
This works great and produces an error of "This field is required" for the group.
My question lies with custom error messages. I have the following setup:
messages: {
fullName: "Please enter both your first name and your last name"
}
Unfortunately the custom error doesn't show, only the generic one.
Does anyone have any ideas?
You have to use errorPlacement for this, and the message should be the same on both, for example:
messages: {
myFirstName: { required: "Please enter both your first name and your last name" },
myLastName: { required: "Please enter both your first name and your last name" }
}
Then, assuming they have the same IDs here, your errorPlacement option would look like this:
errorPlacement: {
var n = element.attr("name");
if (n == "myFirstName" || n == "myLastName")
error.insertAfter("#myLastName");
else
error.insertAfter(element);
}
The group itself has no message, it's just telling the plugin that they share a message label.

How to change the content of "This field is required" in Jquery form validation plugin?

How can I change the general message of "This field is required" in Jquery form validation plugin to "このフィールドは必須です"? The color of the message can be changed by using the following code:
<style type="text/css">
label.error {color: red;}
</style>
But how to change the content?
I want to change all "This filed is required" messages.
I want to change all "required" messages to "このフィールドは必須です".
$(".selector").validate({
rules: {
name: "required",
email: {
required: true,
email: true
}
},
messages: {
name: "Please specify your name",
email: {
required: "We need your email address to contact you",
email: "Your email address must be in the format of name#domain.com"
}
}
})
only changes specific message for specific rule and specific element.
I wrote
messages: {
required:"このフィールドは必須です"
}
but it doesn't work.
I tried the accepted answer and it did not work for me at all. I did more searching on Google and found this article.
Using this line of code solved my problem:
$.validator.messages.required = "Your new required message here!";
The messages object has several interesting attributes to adjust:
messages: {
required: "This field is required.",
remote: "Please fix this field.",
email: "Please enter a valid email address.",
url: "Please enter a valid URL.",
...
}
See the source.
These can be set as defaults via the setDefaults() method:
$.validator.setDefaults({
messages: {
required: "このフィールドは必須です"
}
});
You can use the messages option in the validate method.

Categories