I need a regular field validator that accepts alphabet numbers `-. and only one space between words

I tried using ^ [a-zA-Z0-9 `.] * $. But this allows more spaces. And can anyone explain what is "closed" in this context? Any help is appreciated.

+3


source to share


4 answers


Try the following:

/^[a-z0-9\-\.`]+\s{0,1}[a-z0-9\-\.`]+$/gmi

      




Regex live here.

Clarification:

^[a-z0-9\-\.\`]+       # starts with at least one letter/number/-/./`
\s{0,1}                # must or not contain one space - same as: '\s?'
[a-z0-9\-\.\`]+$       # ends with at least one letter/number/-/./`

      

+3


source


Your curly braces have space in them and are also at the beginning of your regex, after the carrot. so you need to exclude spaces at the beginning and end of the text:

/^([a-z0-9\-]+\s{0,1}[a-z0-9\-]+)+$/gmi

      

you also want to include the '-' character by escaping and including in it.



https://regex101.com/

A good site for testing regular expressions

+1


source


This needs to do a pretty good job:

/*#!(?#!js valid Rev:20150715_1300)
    # Validate alphabets numbers `-. and only one space.
    ^                        # Anchor to start of string.
    (?=[^ ]+(?:[ ][^ ]+)*$)  # Only one space between words.
    [a-zA-Z0-9 `.-]*         # One or more allowed chars.
    $                        # Anchor to end of string.
!#*/
var valid = /^(?=[^ ]+(?: [^ ]+)*$)[a-zA-Z0-9 `.-]*$/;

      

+1


source


What I am interpreting from your question would look something like this.

^([a-zA-Z0-9`.]+ ?)*[a-zA-Z0-9`.]+$

This means that for every space in your string, there must be a series of characters you suggested and it must end with at least one of those characters.

0


source







All Articles