Instead of the below regex:
/^[a-zA-Z]+$/
Use this:
/^[a-zA-Z\s]+$/
This will also take the space.
Answer from Code Lღver on Stack OverflowInstead of the below regex:
/^[a-zA-Z]+$/
Use this:
/^[a-zA-Z\s]+$/
This will also take the space.
Just leave a space or use \s in your regex:
$.validator.addMethod("alpha", function(value, element) {
return this.optional(element) || value == value.match(/^[a-zA-Z\s]+$/);
// -- or leave a space here ^^
});
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");
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
To match the terms:
- Expression can start only with a letter
- Expression can contain letters or spaces
You need to use this regex expression:
/^[a-z][a-z\s]*$/
So in your js it should be:
jQuery.validator.addMethod("letterswithspace", function(value, element) {
return this.optional(element) || /^[a-z][a-z\s]*$/i.test(value);
}, "letters only");
Explanation
^[a-z]means start with one letter[a-z\s]*$means after accept zero or more letters or spaces
Valid sentence
If you want a valid sentence structure:
- Expression can start or end only with a letter
- Expression cannot contain consecutive spaces
use:
/^([a-z]+\s)*[a-z]+$/
By the way
- These regex expressions do not accept capital letters. To add capital letters support instead of
a-zusea-zA-Z
You need to use this regex expression:-
/^[a-zA-Z-,]+(\s{0,1}[a-zA-Z-, ])*$/;
Example Without Jquery Validator using Javascript:- Its Work Fine Javascript Example
Hope Its Work !!
I found the answer after some search.
we can use below regex to achieve this.
/^([\s.]?[a-zA-Z]+)+$/
Complete jQuery function:-
<script>
$(document).ready(function () {
$("input[type='text']").each(function () {
$(this).blur(function (e) {
if (Validate(this.id)) { }
else { alert('invalid input'); }
});
});
function Validate(evt) {
var isValid = false;
var regex = /^([\s\.]?[a-zA-Z]+)+$/;
isValid = regex.test($("#" + evt).val());
return isValid;
}
});
</script>
You can use the following pattern.It will allow only alphabets,dots and spaces/^[a-zA-Z\. ]*$/
otherwise use prevent default method
$(function()
{
$('#username').keydown(function(er)
{
if(er.altKey||er.ctrlKey||er.shiftKey)
{
er.preventDefault();
}
else
{var key=er.keyCode;
if(!((key==8)||(key==9)||(key==32)||(key==46)||(key>=65 && key<=90)))
{
er.preventDefault();
alert("please enter only alphabets")
}
}
}); });
Simply add a custom validator, and use it like this:
jQuery.validator.addMethod("accept", function(value, element, param) {
return value.match(new RegExp("." + param + "$"));
});
Only numbers:
rules: {
field: { accept: "[0-9]+" }
}
Only letters
rules: {
field: { accept: "[a-zA-Z]+" }
}
A small change.
jQuery.validator.addMethod("accept", function(value, element, param) {
return value.match(new RegExp("^" + param + "$"));
});
Because that way it was accepting expressions like "#abc".
You need to add the whitespace character (\s) to your Regex:
jQuery.validator.addMethod("lettersonly", function(value, element) {
return this.optional(element) || /^[a-z\s]+$/i.test(value);
}, "Only alphabetical characters");
jQuery.validator.addMethod("lettersonly", function(value, element) {
return this.optional(element) || /^[a-z\s]+$/i.test(value);
}, "Only alphabetical characters");
and
$('#yourform').validate({
rules: {
name_field: {
lettersonly: true
}
}
});
jQuery.validator.addMethod("alphanumericspecial", function(value, element) {
return this.optional(element) || value == value.match(/^[-a-zA-Z0-9_ ]+$/);
}, "Only letters, Numbers & Space/underscore Allowed.");
jQuery.validator.addMethod("alpha", function(value, element) {
return this.optional(element) || value == value.match(/^[a-zA-Z]+$/);
},"Only Characters Allowed.");
jQuery.validator.addMethod("alphanumeric", function(value, element) {
return this.optional(element) || value == value.match(/^[a-z0-9A-Z#]+$/);
},"Only Characters, Numbers & Hash Allowed.");
U can create functions easily like this bro..
You will need to add extra method to validation library, like that:
$.validator.addMethod("alpha", function(value,element)
{
return this.optional(element) || /^[a-zA-Z]$/i.test(value);
}, "Alphabets only");
Then you can add it to your validation rules.
Otherwise, you can define generic "regexp" rule, as described in this answer