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.

3
  • Can you please elaborate on what the right side of the expression does? (Or what you are trying to compare in general?) Commented Jun 1, 2014 at 10:12
  • underscorejs.org/#intersection Commented Jun 1, 2014 at 10:12
  • @rink.attendant.6 It explodes $value into an array, then intersects it with the caracters list and count the occurrencies (they must be same as $value length Commented Jun 1, 2014 at 10:20

2 Answers 2

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');
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it's much more efficient!
0

Use Underscore.js and try:

_.intersection(A, B);

Look into _.intersection()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.