function validate(id) {
var regex = /^[a-zA-Z ]{2,30}$/;
var ctrl = document.getElemetnById(id);
return regex.test(ctrl.value);
}
Answer from Adeel Ahmed on Stack OverflowJavascript regex: test people's name - Stack Overflow
regex - JavaScript Regular Expression Validation - Stack Overflow
html - How to validate a letter and whitespace only input via JavaScript regular expression - Stack Overflow
javascript regex (username validation) - Stack Overflow
Videos
let result = /^[a-zA-Z ]+$/.test( 'John Doe');
console.log(result);
Throw any symbols you need in the character class. This is why I said be specific about exactly what you want to validate. This regex will not account for accented characters, if you care about that you'd most likely better go with unicode matching.
Try this:
/^(([A-Za-z]+[\-\']?)*([A-Za-z]+)?\s)+([A-Za-z]+[\-\']?)*([A-Za-z]+)?$/
It expects optionally [at least 1 alphabetical character followed by a ' or -] an indefinite number of times. There must be at least one alphabetical character before a required space to ensure we are getting at least the first and last name. This entire pattern is grouped to accept indefinite repetition (for people who like to use all their names, such as John Jacob Jingleheimer Schmidt), but must appear at least once, by means of the + sign right in the middle. Finally, the last name is treated the same way as the other names, but no trailing space is allowed. (Unfortunately this means we are violating DRY a little bit.)
Here is the outcome on several possible pieces of input:
"Jon Doe": true
"Jonathan Taylor Thomas": true
"Julia Louis-Dreyfus": true
"Jean-Paul Sartre": true
"Pat O'Brien": true
"รรณr Eldon": false
"Marcus Wells-O'Shaugnessy": true
"Stephen Wells-O'Shaugnessy Marcus": true
"This-Is-A-Crazy-Name Jones": true
"---- --------": false
"'''' ''''''''": false
"'-'- -'-'-'-'": false
"a-'- b'-'-'-'": false
"'-'c -'-'-'-d": false
"e-'f g'-'-'-h": false
"'ij- -klmnop'": false
Note it still doesn't handle Unicode characters, but it could possibly be expanded to include those if needed.
Try /^[a-z]+\\[a-z]+\\[a-z]+$/
function validateResourceName() {
//get posted resource name value
var inputString = document.getElementById("resourceName").value;
//should be in the word\word\word format
var pattern=/^[a-z]+\\[a-z]+\\[a-z]+$/
//If the inputString is NOT a match
if (!pattern.test(inputString)) {
alert("not a match");
} else {
alert("match");
}
}
If you want to allow the word matching to be case insensitive;
`/^[a-z]+\\[a-z]+\\[a-z]+$/i`
If you want to be a bit more broad with what you define as a 'word', and allow it to consist of alphanumeric characters and underscore;
`/^\w+\\\w+\\\w+$/i`
If by word you mean the English letters a-z in upper or lower case, then:
/^(?:[a-z]+\\){2}[a-z]+$/i
That says:
^Beginning of string(?:...)Non-capturing group[a-z]+One or more letters a-z (or A-Z because of theiflag at the end). If you also want to allow some other characters, just add them to the[a-z]after thez. If you want to allow hyphens, add\-to it (you need the backslash, depending on where you put the hyphen, so I just always include it). Note that this is very English-centric, and even in English sometimes people write borrowed words with their non-English letters, such as rรฉsumรฉ.\\Backslash{2}Repeated twice- (Then another word)
$End of string
The issues with your expression are:
[a-Z]Is invalid because the range is out of order (Zcomes beforea). If it were valid (or if you wrote[Z-a]), it would matches everything betweenZanda, which isn't justa-zandA-Z\\/Requires a backslash and then a slash|is an alternation (this or that)\sis whitespace
You can use javascript test() method to validate name field. The test() method tests for a match in a string.
/^[A-Za-z\s]+$/.test(x) //returns true if matched, vaidates for a-z and A-Z and white space
or
/^[A-Za-z ]+$/.test(x)
If you are building something for modern browsers, there is something very pleasurable in HTML5:
<input id="username" name="name" type="text" pattern="[a-zA-Z]{5,}" title="Minimum 5 letters" required />
Reference: HTML5 forms input types
Update 2017/01/17:
Old browsers are quite weak on the market shares nowadays. This is a good practice to use HTML5 features instead of compatibility scripts.
The code you have looks fine, aside from the inconsistent variable reference (see the comment by Josh Purvis).
The following regex is fine for your first name spec:
var nameRegex = /^[a-zA-Z\-]+$/;
Adding digits for your username check is straightforward:
var usernameRegex = /^[a-zA-Z0-9]+$/;
Note: There are many ways to write regular expressions. I've chosen to provide a version that matches what you started with. I encourage you to work through this Regular Expression Tutorial
Here's the validation function I came up with, tailor it for your own use-cases:
function isUserNameValid(username) {
/*
Usernames can only have:
- Lowercase Letters (a-z)
- Numbers (0-9)
- Dots (.)
- Underscores (_)
*/
const res = /^[a-z0-9_\.]+$/.exec(username);
const valid = !!res;
return valid;
}