admin管理员组

文章数量:1426594

I have id's validation method, each id can contain only [a-z0-9_] case insensitive characters. [^a-z] seems working, but I want to append [^0-9] but it's not work. How one string pattern should looks like?

function foo() {
  testUserIds = ['id23425860', 
  'yulka_rdtska',
  'im?sel=2000000001',
  'im?sel=2000000002'];
  for (id in testUserIds) {
    document.write(isUserIdValid(id) + '\r\n');
  }
}
function isUserIdValid(user_id) {
  var patt = new RegExp('[^a-z]', 'ig');
  return patt.test(user_id);
}

I have id's validation method, each id can contain only [a-z0-9_] case insensitive characters. [^a-z] seems working, but I want to append [^0-9] but it's not work. How one string pattern should looks like?

function foo() {
  testUserIds = ['id23425860', 
  'yulka_rdtska',
  'im?sel=2000000001',
  'im?sel=2000000002'];
  for (id in testUserIds) {
    document.write(isUserIdValid(id) + '\r\n');
  }
}
function isUserIdValid(user_id) {
  var patt = new RegExp('[^a-z]', 'ig');
  return patt.test(user_id);
}
Share Improve this question asked May 7, 2012 at 20:20 VasyaVasya 2591 gold badge5 silver badges15 bronze badges 3
  • [^a-z0-9]. Put it inside the same character class. – Michael Berkowski Commented May 7, 2012 at 20:22
  • I've tried that, it gives me wrong results: false false false false – Vasya Commented May 7, 2012 at 20:24
  • Oh, man, sorry there just my erro in for – Vasya Commented May 7, 2012 at 20:25
Add a ment  | 

5 Answers 5

Reset to default 3

This is what you need:

function isUserIdValid(user_id) {
  var patt = new RegExp('[^a-z0-9_]', 'ig');
  return patt.test(user_id);
}

The problem is that you are using a for-in construct instead of a proper for loop. Use [^a-z0-9_] as your regular expression and iterate correctly over your array.

In JavaScript, don't iterate over arrays with for (elem in arr), as that syntax's purpose is to iterate over properties of an object. Instead use for (var idx=0; idx<something; idx++).

function foo() {
  testUserIds = ['id23425860', 
  'yulka_rdtska',
  'im?sel=2000000001',
  'im?sel=2000000002'];

  // Use an incremental for loop here, NOT a for-in
  // for-in is intended for iterating over object properties, not arrays.
  for (var i=0; i<testUserIds.length; i++) {
    console.log(isUserIdValid(testUserIds[i]) + '\r\n');
  }
}
function isUserIdValid(user_id) {
  var patt = new RegExp('[^a-z0-9_]', 'ig');

  // Return the inversion of this, so isUserValid() is true if the user is valid
  return !patt.test(user_id);
}

foo();
// Outputs
true
true
false
false

Note that the way you have setup your function, it returns the opposite of what it says. You are checking for invalid characters, so return the inverted:

// If it matched, it's invalid, otherwise valid
return !patt.test(user_id);

Something like this should work:

var patt = new RegExp(/^[0-9]+$/, "ig");
function isUserIdValid(user_id) {
    var patt = new RegExp(/^[a-z0-9]+$/i);
    return patt.test(user_id);
}

You should declare testUserIds with var so that it's a local variable, not a global one.

function foo() {
  var testUserIds = ['id23425860', 
  'yulka_rdtska',
  'im?sel=2000000001',
  'im?sel=2000000002'];

  // rest of your code
}

Also to declare a regular expression you can wrap the expression with slashes which is cleaner, like so:

var patt = /[^a-z]/ig;

A bit off-topic, but I couldn't help pointing out.

本文标签: How to apply 09 regex pattern javascriptStack Overflow