Regex for Only Allowing Letters, Numbers, Space, Commas, Periods

Regex for only allowing letters, numbers, space, commas, periods?

^[\.a-zA-Z0-9,!? ]*$

is what the regex is, it works, see example website to test regex.

Regex - Allow alphanumeric, comma, period and space using PHP

Are you sure the issue isn't the ^, *, and $ at the end? Your character class is correct, but what you're matching is "beginning of line, then 0 or more of [a-zA-Z0-9,. ], then the end of the line".

What this means is that it'll match if there's nothing in the string (0 or more of your characters), but not if there is anything other than those characters (because it's anchored to the beginning and end of the line). If that's really what you want to know (if there is anything other than those characters), then you can reverse the character class like this, and remove the ^, *, and $: [^a-zA-Z0-9,. ].

Otherwise (if you want to know if the string has any of those characters), you should just remove everything but the character class: '/[a-zA-Z0-9,. ]/'.

Regex for numbers only allowing spaces and 1 comma or dot

You could use a character class to match either a dot or a comma followed by 1+ digits in an optional group:

^\d+(?:[,.]\d+)?(?: \d+(?:[,.]\d+)?)*$

That will match

  • ^ Start of the string
  • \d+(?:[,.]\d+)? Match 1+ digits with an optional part to match either a dot or a comma followed by 1+ digits
  • (?: \d+(?:[,.]\d+)?)* repeat the previous part 0+ times with a space at the start
  • $ End of the string

Regex demo

Update

If the string can only contain a single dot or comma:

^(?!(?:[^.,\n]*[.,]){2})\d+(?:[,.]\d+)?(?:[^\S\n\r]\d+(?:[,.]\d+)?)*$
  • ^ Start of the string
  • (?!(?:[^.,\n]*[.,]){2}) Negative lookahead, assert what is on the right is not 2 times a dot or comma
  • \d+(?:[,.]\d+)? Match 1+ digits, optionally match a dot or comma and 1+ digits
  • (?: Non capturing group

    • [^\S\n\r] Negated character class, not match a non-whitespace char and not match a newline
    • \d+(?:[,.]\d+)? Match 1+ digits, optionally match a dot or comma and 1+ digits
    • )* Close non capturing group and repeat 0+ times
  • $ End of the string

Regex demo

let regex = /^(?!(?:[^.,\n]*[.,]){2})\d+(?:[,.]\d+)?(?:[^\S\n\r]\d+(?:[,.]\d+)?)*$/;[  "424242424",  "424242.424",  "454545.58",  "4545454,4545",  "45454,45",  "4 45 44454.45",  "4 454 454,45",  "1 2 3",  "1 2 3",  "4,2 424 484,45 34.4 3 4",  "545g45",  "454.454.45",  "45454,454,45",  "45.44,45",  "45,45.45",  "4 4545 4545 45  45",].forEach(s => console.log(s + ": " + regex.test(s)))

javascript regex to allow letters, numbers, periods, hyphens, underscores, and spaces

Your if statement is reversed. You should check when the regex DOES NOT match instead:

!regexp1.test(document.getElementById("url").value)

Also I believe the original regex is wrong/inaccurate try the one shown below:

function process() {    var regexp1=new RegExp("^[0-9A-Za-z_.-]+$");
var url = "http://www.website.com/page.php?data=" + document.getElementById("url").value;
if (!regexp1.test(document.getElementById("url").value)) { console.log("Only numbers, letters, hypens, periods, spaces and underscores are allowed"); } else { console.log("Passed validation."); }}
<input type="text" size="10" maxlength="30" name="url" id="url"><input type="button" onclick="process()">

Regular Expression: Letters, numbers, spaces, hyphens, periods, and underscores only and must begin with a letter

Your regex looks ok to me. Perhaps there is a problem with the surrounding code rather than the regex? This is a Python example:

s_re = re.compile('^[a-z][a-z0-9_.-]*',re.I) # case insensitive match

In [12]: if s_re.match('A'): print 'match'
match

In [14]: if s_re.match('A.-'): print 'match'
match

In [15]: if s_re.match('1.-'): print 'match'

In [16]: if s_re.match('1_.-'): print 'match'

In [17]: if s_re.match('A_.-'): print 'match'
match

If you want to make sure you want at least one character after the first letter, you can replace the * with a +, or {2,} with at least 2 more characters, or {2,5} with between 2 and 5 characters.

Regex to allow insensitive alphabets, numbers and special chars like comma, dot, hash and hypen

The following regex works for US addresses like "928 Mill Samy Dr, Apt #23AB-23" or "P.O Box #2323" /^[a-zA-Z0-9,#\-.\s]*$/i

RegEx start and finish with letter, allow commas and dashes

You may use

/^[a-zøåæäöüß][a-z0-9øåæäöüß]*(?:[-,][a-zøåæäöüß][a-z0-9øåæäöüß]*)*$/i

See the regex demo

Details:

  • ^ - start of string
  • [a-zøåæäöüß] - a letter from the defined set
  • [a-z0-9øåæäöüß]* - 0+ digits or letters from the defined set
  • (?:[-,][a-zøåæäöüß][a-z0-9øåæäöüß]*)* - zero or more sequences of:

    • [-,] - a - or ,
    • [a-zøåæäöüß] - a letter from the defined set
    • [a-z0-9øåæäöüß]* - 0+ digits or letters from the defined set
  • $ - end of string.

Regex to match numbers, commas, dots, spaces in a given string

You might use a pattern to first match the digits and optionally match either a space, comma or dot followed by 1+ digit so that the dot comma or space can not be at the end.

\d+(?:[,. ]\d+)*
  • \d+ Match 1+ digits
  • (?: Non capture group
    • [,. ]\d+ Match either a space , or . and 1+ digits
  • )* Close group and repeat 0+ times

Regex demo

A bit more precise match could be

\b\d{1,3}(?:[,. ]\d{3})*(?:[.,]\d{2})?\b

Regex demo

Regex for allowing alphanumeric,-,_ and space

Try this regex:

/^[a-z\d\-_\s]+$/i


Related Topics



Leave a reply



Submit