I'm attempting to validate a field name to match a certain format in JavaScript using Regular Expressions.

I need the string inputted to resemble this:
word\word\word
So anything inputted can't be blank, and it must be three words seperated by a backslash.

This is the code i'm working with, but i'm not sure if the pattern is the right syntax?!!

 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\s]/; //If the inputString is NOT a match if (!pattern.test(inputString)) { alert("not a match"); } else { alert("match"); } } 


Any help will be very appreciated!!!

1

5 Answers

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 the i flag at the end). If you also want to allow some other characters, just add them to the [a-z] after the z. 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 (Z comes before a). If it were valid (or if you wrote [Z-a]), it would matches everything between Z and a, which isn't just a-z and A-Z
  • \\/ Requires a backslash and then a slash
  • | is an alternation (this or that)
  • \s is whitespace
3

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` 

you can just use this \w+\\\w+\\\w+

or

[a-zA-Z]+(\\[a-zA-Z]+){2} 
5

Try this one , See the Regex fiddle for regex demo and Jsfiddle for the code demo

Regex

/(\w)*\\(?!\\)(\w)*\\(?!\\)(\w)*(?!\\)/g 

Javascript

 function validateResourceName(string) { var pattern = /(\w)*\\(?!\\)(\w)*\\(?!\\)(\w)*(?!\\)/g; if (!pattern.test(string)) { alert("not a match"); } else { alert("match"); } } 
1

This should do it

^\w+\\\w+\\\w+$ 

In javascript

if (/^\w+\\\w+\\\w+$/.test(subject)) { // Successful match } else { // Match attempt failed } 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy