2

I have this PHP function to compare a username $value with a characters list:

strlen($value) == count(array_intersect(array_map("strtoupper", str_split($value)), str_split("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-")))

I need to obtain something similar using Jquery. I have problems with array_intersect() at the moment, I can't find any similar function.
Thanks for any answer.

Xriuk
  • 382
  • 2
  • 7
  • 26

2 Answers2

2

Stack Overflow has already questions about array intersection in JavaScript.

You may also be interested by intersection function in Underscore.js.

Finally, wouldn't it be easier here to use regular expressions? In PHP, your code becomes as simple as:

$isValidUserName = preg_match('/^[A-Z_\-]+$/i', $value);

In JavaScript:

var isValidUserName = (/^[A-Z_\-]*$/i).test('Hello_World');
Community
  • 1
  • 1
Arseni Mourzenko
  • 50,338
  • 35
  • 112
  • 199
0

Use Underscore.js and try:

_.intersection(A, B);

Look into _.intersection()

martynas
  • 12,120
  • 3
  • 55
  • 60