I made a regular expression that only accepts letters. I'm not really good in regex, thats why I don't know how to include spaces in my regex.
My HTML:
<input id="input" />
My js / jQuery code:
$('#input').on('keyup', function() {
var RegExpression = /^[a-zA-Z]*$/;
if (RegExpression.test($('#input').val())) {
}
else {
$('#input').val("");
}
});?
This question is related to
javascript
jquery
Try this demo please: http://jsfiddle.net/sgpw2/
Thanks Jan for spaces \s
rest there is some good detail in this link:
http://www.jquery4u.com/syntax/jquery-basic-regex-selector-examples/#.UHKS5UIihlI
Hope it fits your need :)
code
$(function() {
$("#field").bind("keyup", function(event) {
var regex = /^[a-zA-Z\s]+$/;
if (regex.test($("#field").val())) {
$('.validation').html('valid');
} else {
$('.validation').html("FAIL regex");
}
});
});?
$('#input').on('keyup', function() {
var RegExpression = /^[a-zA-Z\s]*$/;
...
});
\s
will allow the space
Allowed only characters & spaces. Ex : Jayant Lonari
if (!/^[a-zA-Z\s]+$/.test(NAME)) {
//Throw Error
}
Source: Stackoverflow.com