1

Looking for a Javascript validation regular expression that validates both of these case

  • Characters OR characters + numbers
  • No Standalone numbers

Thanks,

kiranvj
  • 32,342
  • 7
  • 71
  • 76

4 Answers4

0

try use this regex ^[\da-zA-Z]*?[a-zA-Z]+[\da-zA-Z]*?$

burning_LEGION
  • 13,246
  • 8
  • 40
  • 52
0

How about this?

var tests = [
  'fsdfdsfASAS34csdfsd', 
  'dadsd', 
  '332'
]; // add here whatever you like to test

var re = /^(?=.*[a-z])[0-9a-z]+$/i; 
// with [0-9a-z]+ we test that string contains only alphanumericals,
// and with (?=.*[a-z]) we test that it has at least one [a-zA-Z] character present

for (var i = 0, l = tests.length; i < l; ++i) {
  if (re.test(tests[i])) {
    console.log(tests[i] + ' passed');
  }
  else {
    console.log(tests[i] + ' failed');
  }
}
raina77ow
  • 103,633
  • 15
  • 192
  • 229
0

Try this

(?i)\b([a-z0-9]*[a-z][a-z0-9]*)\b

Explanation

(?i)           # Match the remainder of the regex with the options: case insensitive (i)
\b             # Assert position at a word boundary
(              # Match the regular expression below and capture its match into backreference number 1
   [a-z0-9]       # Match a single character present in the list below
                     # A character in the range between “a” and “z”
                     # A character in the range between “0” and “9”
      *              # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
   [a-z]          # Match a single character in the range between “a” and “z”
   [a-z0-9]       # Match a single character present in the list below
                     # A character in the range between “a” and “z”
                     # A character in the range between “0” and “9”
      *              # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)
\b             # Assert position at a word boundary

or

(?is)^([a-z0-9]*[a-z][a-z0-9]*)$

Explanation

(?is)          # Match the remainder of the regex with the options: case insensitive (i); dot matches newline (s)
^              # Assert position at the beginning of the string
(              # Match the regular expression below and capture its match into backreference number 1
   [a-z0-9]       # Match a single character present in the list below
                     # A character in the range between “a” and “z”
                     # A character in the range between “0” and “9”
      *              # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
   [a-z]          # Match a single character in the range between “a” and “z”
   [a-z0-9]       # Match a single character present in the list below
                     # A character in the range between “a” and “z”
                     # A character in the range between “0” and “9”
      *              # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)
$              # Assert position at the end of the string (or before the line break at the end of the string, if any)
Cylian
  • 10,970
  • 4
  • 42
  • 55
0
/(?=[^0-9][a-zA-Z0-9])/

Is your magic regex.

[ 'ads', '3ds', '3' ].map( function( c ) {
    return /(?=[^0-9][a-zA-Z0-9])/.test( c );
});

[true, true, false]

(?= is a way to say AND for the square brackets. [^0-9] excludes numbers only, [a-zA-Z0-9] allows letters only, and letters + numbers.

Community
  • 1
  • 1
Florian Margaine
  • 58,730
  • 15
  • 91
  • 116