jQuery validation plugin: accept only alphabetical characters?
up vote
14
down vote
favorite
I'd like to use jQuery's validation plugin to validate a field that only accepts alphabetical characters, but there doesn't seem to be a defined rule for it. I've searched google but I've found nothing useful.
Any ideas?
Appreciate your help.
jquery validation jquery-validate
add a comment |
up vote
14
down vote
favorite
I'd like to use jQuery's validation plugin to validate a field that only accepts alphabetical characters, but there doesn't seem to be a defined rule for it. I've searched google but I've found nothing useful.
Any ideas?
Appreciate your help.
jquery validation jquery-validate
add a comment |
up vote
14
down vote
favorite
up vote
14
down vote
favorite
I'd like to use jQuery's validation plugin to validate a field that only accepts alphabetical characters, but there doesn't seem to be a defined rule for it. I've searched google but I've found nothing useful.
Any ideas?
Appreciate your help.
jquery validation jquery-validate
I'd like to use jQuery's validation plugin to validate a field that only accepts alphabetical characters, but there doesn't seem to be a defined rule for it. I've searched google but I've found nothing useful.
Any ideas?
Appreciate your help.
jquery validation jquery-validate
jquery validation jquery-validate
edited Oct 21 '10 at 22:45
Pavel Chuchuva
18.6k67893
18.6k67893
asked May 8 '10 at 13:05
KeyStroke
77231524
77231524
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
up vote
75
down vote
accepted
If you include the additional methods file, here's the current file for 1.7: http://ajax.microsoft.com/ajax/jquery.validate/1.7/additional-methods.js
You can use the lettersonly
rule :) The additional methods are part of the zip you download, you can always find the latest here.
Here's an example:
$("form").validate({
rules: {
myField: { lettersonly: true }
}
});
It's worth noting, each additional method is independent, you can include that specific one, just place this before your .validate()
call:
jQuery.validator.addMethod("lettersonly", function(value, element) {
return this.optional(element) || /^[a-z]+$/i.test(value);
}, "Letters only please");
^[A-Za-z]+$ to also accept capital letters, or set value to lower case with toLowerCase() function.
– c-chavez
Sep 24 '17 at 13:45
upvoted forthis.optional(element) ||
– Pankaj
Jan 19 at 20:01
add a comment |
up vote
14
down vote
Be careful,
jQuery.validator.addMethod("lettersonly", function(value, element)
{
return this.optional(element) || /^[a-z," "]+$/i.test(value);
}, "Letters and spaces only please");
[a-z, " "] by adding the comma and quotation marks, you are allowing spaces, commas and quotation marks into the input box.
For spaces + text, just do this:
jQuery.validator.addMethod("lettersonly", function(value, element)
{
return this.optional(element) || /^[a-z ]+$/i.test(value);
}, "Letters and spaces only please");
[a-z ] this allows spaces aswell as text only.
............................................................................
also the message "Letters and spaces only please" is not required, if you already have a message in messages:
messages:{
firstname:{
required: "Enter your first name",
minlength: jQuery.format("Enter at least (2) characters"),
maxlength:jQuery.format("First name too long more than (80) characters"),
lettersonly:jQuery.format("letters only mate")
},
Adam
add a comment |
up vote
4
down vote
Just a small addition to Nick's answer (which works great !) :
If you want to allow spaces in between letters, for example ,
you may restrict to enter only letters in full name, but it should allow space as well - just list the space as below with a comma. And in the same way if you need to allow any other specific character:
jQuery.validator.addMethod("lettersonly", function(value, element)
{
return this.optional(element) || /^[a-z," "]+$/i.test(value);
}, "Letters and spaces only please");
add a comment |
up vote
2
down vote
Both Nick and Adam's answers work really well.
I'd like to add a note if you want to allow latin characters like á
and ç
as I wanted to do:
jQuery.validator.addMethod('lettersonly', function(value, element) {
return this.optional(element) || /^[a-z áãâäàéêëèíîïìóõôöòúûüùçñ]+$/i.test(value);
}, "Letters and spaces only please");
1
Thanks! but u also forgot ñ hehe :)
– Andres
Nov 22 '16 at 15:20
Thanks, @Andres! I just updated my answer and addedñ
– FlavioEscobar
Nov 23 '16 at 17:53
add a comment |
up vote
1
down vote
to allow letters ans spaces
jQuery.validator.addMethod("lettersonly", function(value, element) {
return this.optional(element) || /^[a-zs]+$/i.test(value);
}, "Only alphabetical characters");
$('#yourform').validate({
rules: {
name_field: {
lettersonly: true
}
}
});
add a comment |
up vote
0
down vote
$('.AlphabetsOnly').keypress(function (e) {
var regex = new RegExp(/^[a-zA-Zs]+$/);
var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
if (regex.test(str)) {
return true;
}
else {
e.preventDefault();
return false;
}
});
add a comment |
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
75
down vote
accepted
If you include the additional methods file, here's the current file for 1.7: http://ajax.microsoft.com/ajax/jquery.validate/1.7/additional-methods.js
You can use the lettersonly
rule :) The additional methods are part of the zip you download, you can always find the latest here.
Here's an example:
$("form").validate({
rules: {
myField: { lettersonly: true }
}
});
It's worth noting, each additional method is independent, you can include that specific one, just place this before your .validate()
call:
jQuery.validator.addMethod("lettersonly", function(value, element) {
return this.optional(element) || /^[a-z]+$/i.test(value);
}, "Letters only please");
^[A-Za-z]+$ to also accept capital letters, or set value to lower case with toLowerCase() function.
– c-chavez
Sep 24 '17 at 13:45
upvoted forthis.optional(element) ||
– Pankaj
Jan 19 at 20:01
add a comment |
up vote
75
down vote
accepted
If you include the additional methods file, here's the current file for 1.7: http://ajax.microsoft.com/ajax/jquery.validate/1.7/additional-methods.js
You can use the lettersonly
rule :) The additional methods are part of the zip you download, you can always find the latest here.
Here's an example:
$("form").validate({
rules: {
myField: { lettersonly: true }
}
});
It's worth noting, each additional method is independent, you can include that specific one, just place this before your .validate()
call:
jQuery.validator.addMethod("lettersonly", function(value, element) {
return this.optional(element) || /^[a-z]+$/i.test(value);
}, "Letters only please");
^[A-Za-z]+$ to also accept capital letters, or set value to lower case with toLowerCase() function.
– c-chavez
Sep 24 '17 at 13:45
upvoted forthis.optional(element) ||
– Pankaj
Jan 19 at 20:01
add a comment |
up vote
75
down vote
accepted
up vote
75
down vote
accepted
If you include the additional methods file, here's the current file for 1.7: http://ajax.microsoft.com/ajax/jquery.validate/1.7/additional-methods.js
You can use the lettersonly
rule :) The additional methods are part of the zip you download, you can always find the latest here.
Here's an example:
$("form").validate({
rules: {
myField: { lettersonly: true }
}
});
It's worth noting, each additional method is independent, you can include that specific one, just place this before your .validate()
call:
jQuery.validator.addMethod("lettersonly", function(value, element) {
return this.optional(element) || /^[a-z]+$/i.test(value);
}, "Letters only please");
If you include the additional methods file, here's the current file for 1.7: http://ajax.microsoft.com/ajax/jquery.validate/1.7/additional-methods.js
You can use the lettersonly
rule :) The additional methods are part of the zip you download, you can always find the latest here.
Here's an example:
$("form").validate({
rules: {
myField: { lettersonly: true }
}
});
It's worth noting, each additional method is independent, you can include that specific one, just place this before your .validate()
call:
jQuery.validator.addMethod("lettersonly", function(value, element) {
return this.optional(element) || /^[a-z]+$/i.test(value);
}, "Letters only please");
edited May 8 '10 at 13:20
answered May 8 '10 at 13:10
Nick Craver♦
527k11511891097
527k11511891097
^[A-Za-z]+$ to also accept capital letters, or set value to lower case with toLowerCase() function.
– c-chavez
Sep 24 '17 at 13:45
upvoted forthis.optional(element) ||
– Pankaj
Jan 19 at 20:01
add a comment |
^[A-Za-z]+$ to also accept capital letters, or set value to lower case with toLowerCase() function.
– c-chavez
Sep 24 '17 at 13:45
upvoted forthis.optional(element) ||
– Pankaj
Jan 19 at 20:01
^[A-Za-z]+$ to also accept capital letters, or set value to lower case with toLowerCase() function.
– c-chavez
Sep 24 '17 at 13:45
^[A-Za-z]+$ to also accept capital letters, or set value to lower case with toLowerCase() function.
– c-chavez
Sep 24 '17 at 13:45
upvoted for
this.optional(element) ||
– Pankaj
Jan 19 at 20:01
upvoted for
this.optional(element) ||
– Pankaj
Jan 19 at 20:01
add a comment |
up vote
14
down vote
Be careful,
jQuery.validator.addMethod("lettersonly", function(value, element)
{
return this.optional(element) || /^[a-z," "]+$/i.test(value);
}, "Letters and spaces only please");
[a-z, " "] by adding the comma and quotation marks, you are allowing spaces, commas and quotation marks into the input box.
For spaces + text, just do this:
jQuery.validator.addMethod("lettersonly", function(value, element)
{
return this.optional(element) || /^[a-z ]+$/i.test(value);
}, "Letters and spaces only please");
[a-z ] this allows spaces aswell as text only.
............................................................................
also the message "Letters and spaces only please" is not required, if you already have a message in messages:
messages:{
firstname:{
required: "Enter your first name",
minlength: jQuery.format("Enter at least (2) characters"),
maxlength:jQuery.format("First name too long more than (80) characters"),
lettersonly:jQuery.format("letters only mate")
},
Adam
add a comment |
up vote
14
down vote
Be careful,
jQuery.validator.addMethod("lettersonly", function(value, element)
{
return this.optional(element) || /^[a-z," "]+$/i.test(value);
}, "Letters and spaces only please");
[a-z, " "] by adding the comma and quotation marks, you are allowing spaces, commas and quotation marks into the input box.
For spaces + text, just do this:
jQuery.validator.addMethod("lettersonly", function(value, element)
{
return this.optional(element) || /^[a-z ]+$/i.test(value);
}, "Letters and spaces only please");
[a-z ] this allows spaces aswell as text only.
............................................................................
also the message "Letters and spaces only please" is not required, if you already have a message in messages:
messages:{
firstname:{
required: "Enter your first name",
minlength: jQuery.format("Enter at least (2) characters"),
maxlength:jQuery.format("First name too long more than (80) characters"),
lettersonly:jQuery.format("letters only mate")
},
Adam
add a comment |
up vote
14
down vote
up vote
14
down vote
Be careful,
jQuery.validator.addMethod("lettersonly", function(value, element)
{
return this.optional(element) || /^[a-z," "]+$/i.test(value);
}, "Letters and spaces only please");
[a-z, " "] by adding the comma and quotation marks, you are allowing spaces, commas and quotation marks into the input box.
For spaces + text, just do this:
jQuery.validator.addMethod("lettersonly", function(value, element)
{
return this.optional(element) || /^[a-z ]+$/i.test(value);
}, "Letters and spaces only please");
[a-z ] this allows spaces aswell as text only.
............................................................................
also the message "Letters and spaces only please" is not required, if you already have a message in messages:
messages:{
firstname:{
required: "Enter your first name",
minlength: jQuery.format("Enter at least (2) characters"),
maxlength:jQuery.format("First name too long more than (80) characters"),
lettersonly:jQuery.format("letters only mate")
},
Adam
Be careful,
jQuery.validator.addMethod("lettersonly", function(value, element)
{
return this.optional(element) || /^[a-z," "]+$/i.test(value);
}, "Letters and spaces only please");
[a-z, " "] by adding the comma and quotation marks, you are allowing spaces, commas and quotation marks into the input box.
For spaces + text, just do this:
jQuery.validator.addMethod("lettersonly", function(value, element)
{
return this.optional(element) || /^[a-z ]+$/i.test(value);
}, "Letters and spaces only please");
[a-z ] this allows spaces aswell as text only.
............................................................................
also the message "Letters and spaces only please" is not required, if you already have a message in messages:
messages:{
firstname:{
required: "Enter your first name",
minlength: jQuery.format("Enter at least (2) characters"),
maxlength:jQuery.format("First name too long more than (80) characters"),
lettersonly:jQuery.format("letters only mate")
},
Adam
answered Jul 24 '15 at 3:48
adam
14112
14112
add a comment |
add a comment |
up vote
4
down vote
Just a small addition to Nick's answer (which works great !) :
If you want to allow spaces in between letters, for example ,
you may restrict to enter only letters in full name, but it should allow space as well - just list the space as below with a comma. And in the same way if you need to allow any other specific character:
jQuery.validator.addMethod("lettersonly", function(value, element)
{
return this.optional(element) || /^[a-z," "]+$/i.test(value);
}, "Letters and spaces only please");
add a comment |
up vote
4
down vote
Just a small addition to Nick's answer (which works great !) :
If you want to allow spaces in between letters, for example ,
you may restrict to enter only letters in full name, but it should allow space as well - just list the space as below with a comma. And in the same way if you need to allow any other specific character:
jQuery.validator.addMethod("lettersonly", function(value, element)
{
return this.optional(element) || /^[a-z," "]+$/i.test(value);
}, "Letters and spaces only please");
add a comment |
up vote
4
down vote
up vote
4
down vote
Just a small addition to Nick's answer (which works great !) :
If you want to allow spaces in between letters, for example ,
you may restrict to enter only letters in full name, but it should allow space as well - just list the space as below with a comma. And in the same way if you need to allow any other specific character:
jQuery.validator.addMethod("lettersonly", function(value, element)
{
return this.optional(element) || /^[a-z," "]+$/i.test(value);
}, "Letters and spaces only please");
Just a small addition to Nick's answer (which works great !) :
If you want to allow spaces in between letters, for example ,
you may restrict to enter only letters in full name, but it should allow space as well - just list the space as below with a comma. And in the same way if you need to allow any other specific character:
jQuery.validator.addMethod("lettersonly", function(value, element)
{
return this.optional(element) || /^[a-z," "]+$/i.test(value);
}, "Letters and spaces only please");
edited Jan 13 '14 at 14:21
Bruno Lange
630710
630710
answered Dec 29 '13 at 20:53
Steer360
4122613
4122613
add a comment |
add a comment |
up vote
2
down vote
Both Nick and Adam's answers work really well.
I'd like to add a note if you want to allow latin characters like á
and ç
as I wanted to do:
jQuery.validator.addMethod('lettersonly', function(value, element) {
return this.optional(element) || /^[a-z áãâäàéêëèíîïìóõôöòúûüùçñ]+$/i.test(value);
}, "Letters and spaces only please");
1
Thanks! but u also forgot ñ hehe :)
– Andres
Nov 22 '16 at 15:20
Thanks, @Andres! I just updated my answer and addedñ
– FlavioEscobar
Nov 23 '16 at 17:53
add a comment |
up vote
2
down vote
Both Nick and Adam's answers work really well.
I'd like to add a note if you want to allow latin characters like á
and ç
as I wanted to do:
jQuery.validator.addMethod('lettersonly', function(value, element) {
return this.optional(element) || /^[a-z áãâäàéêëèíîïìóõôöòúûüùçñ]+$/i.test(value);
}, "Letters and spaces only please");
1
Thanks! but u also forgot ñ hehe :)
– Andres
Nov 22 '16 at 15:20
Thanks, @Andres! I just updated my answer and addedñ
– FlavioEscobar
Nov 23 '16 at 17:53
add a comment |
up vote
2
down vote
up vote
2
down vote
Both Nick and Adam's answers work really well.
I'd like to add a note if you want to allow latin characters like á
and ç
as I wanted to do:
jQuery.validator.addMethod('lettersonly', function(value, element) {
return this.optional(element) || /^[a-z áãâäàéêëèíîïìóõôöòúûüùçñ]+$/i.test(value);
}, "Letters and spaces only please");
Both Nick and Adam's answers work really well.
I'd like to add a note if you want to allow latin characters like á
and ç
as I wanted to do:
jQuery.validator.addMethod('lettersonly', function(value, element) {
return this.optional(element) || /^[a-z áãâäàéêëèíîïìóõôöòúûüùçñ]+$/i.test(value);
}, "Letters and spaces only please");
edited Nov 23 '16 at 17:52
answered Sep 8 '15 at 17:38
FlavioEscobar
460614
460614
1
Thanks! but u also forgot ñ hehe :)
– Andres
Nov 22 '16 at 15:20
Thanks, @Andres! I just updated my answer and addedñ
– FlavioEscobar
Nov 23 '16 at 17:53
add a comment |
1
Thanks! but u also forgot ñ hehe :)
– Andres
Nov 22 '16 at 15:20
Thanks, @Andres! I just updated my answer and addedñ
– FlavioEscobar
Nov 23 '16 at 17:53
1
1
Thanks! but u also forgot ñ hehe :)
– Andres
Nov 22 '16 at 15:20
Thanks! but u also forgot ñ hehe :)
– Andres
Nov 22 '16 at 15:20
Thanks, @Andres! I just updated my answer and added
ñ
– FlavioEscobar
Nov 23 '16 at 17:53
Thanks, @Andres! I just updated my answer and added
ñ
– FlavioEscobar
Nov 23 '16 at 17:53
add a comment |
up vote
1
down vote
to allow letters ans spaces
jQuery.validator.addMethod("lettersonly", function(value, element) {
return this.optional(element) || /^[a-zs]+$/i.test(value);
}, "Only alphabetical characters");
$('#yourform').validate({
rules: {
name_field: {
lettersonly: true
}
}
});
add a comment |
up vote
1
down vote
to allow letters ans spaces
jQuery.validator.addMethod("lettersonly", function(value, element) {
return this.optional(element) || /^[a-zs]+$/i.test(value);
}, "Only alphabetical characters");
$('#yourform').validate({
rules: {
name_field: {
lettersonly: true
}
}
});
add a comment |
up vote
1
down vote
up vote
1
down vote
to allow letters ans spaces
jQuery.validator.addMethod("lettersonly", function(value, element) {
return this.optional(element) || /^[a-zs]+$/i.test(value);
}, "Only alphabetical characters");
$('#yourform').validate({
rules: {
name_field: {
lettersonly: true
}
}
});
to allow letters ans spaces
jQuery.validator.addMethod("lettersonly", function(value, element) {
return this.optional(element) || /^[a-zs]+$/i.test(value);
}, "Only alphabetical characters");
$('#yourform').validate({
rules: {
name_field: {
lettersonly: true
}
}
});
answered Jul 12 '16 at 11:13
Sameera Prasad Jayasinghe
455411
455411
add a comment |
add a comment |
up vote
0
down vote
$('.AlphabetsOnly').keypress(function (e) {
var regex = new RegExp(/^[a-zA-Zs]+$/);
var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
if (regex.test(str)) {
return true;
}
else {
e.preventDefault();
return false;
}
});
add a comment |
up vote
0
down vote
$('.AlphabetsOnly').keypress(function (e) {
var regex = new RegExp(/^[a-zA-Zs]+$/);
var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
if (regex.test(str)) {
return true;
}
else {
e.preventDefault();
return false;
}
});
add a comment |
up vote
0
down vote
up vote
0
down vote
$('.AlphabetsOnly').keypress(function (e) {
var regex = new RegExp(/^[a-zA-Zs]+$/);
var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
if (regex.test(str)) {
return true;
}
else {
e.preventDefault();
return false;
}
});
$('.AlphabetsOnly').keypress(function (e) {
var regex = new RegExp(/^[a-zA-Zs]+$/);
var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
if (regex.test(str)) {
return true;
}
else {
e.preventDefault();
return false;
}
});
answered May 23 '17 at 12:35
Anup Shetty
30838
30838
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2794162%2fjquery-validation-plugin-accept-only-alphabetical-characters%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown