1

I am using criso validator.js, of user Input,

but it Eslint is showing error in syntax on this lines

if (!Validator.isAlphanumeric([(data.password,'en-US')])) {
    console.log(" Not an alphanumeric");
  }

how to properly check user's entered password is Alphanumeric,

I know we can do it using regex but I wanted to do it by using their provided syntax as isAlphanumeric(str [, locale]).

here is their documentation screenshot of code.

screenshot

GD- Ganesh Deshmukh
  • 1,456
  • 3
  • 24
  • 36
  • Why only alpha numeric? The hash algorithm used for hashing the password will work the same way with or without "special" characters. – Andreas Sep 04 '18 at 15:09
  • 1
    There's excessive square brackets. Correct one should be like this `Validator.isAlphanumeric(data.password,'en-US')` I think – Andrey Sep 04 '18 at 15:11
  • 1
    Forcing passwords to be alphanumeric just makes them weaker. It is a really bad idea. – Quentin Sep 04 '18 at 15:27
  • 1
    @Andrey is right. Also you should consider [password-validator](https://github.com/tarunbatra/password-validator) for password validation rules. – tbking Sep 04 '18 at 15:28
  • @Andrey, your solution worked. but the same answer is given by Romellem.. , after your comment. – GD- Ganesh Deshmukh Sep 04 '18 at 15:33

1 Answers1

4

The square brackets in the isAlphanumeric(str [, locale]) notation are not related to JavaScript Array literals. Instead, they denote that when calling isAlphanumeric, the first argument str (in your case, data.password) is required, and the second argument locale is optional.

In your case, you do want to pass in a locale. Here is how that would look:

if (!Validator.isAlphanumeric(data.password, 'en-US')) {
    console.log("Not an alphanumeric");
}

In technical documentation, square brackets ([]) generally denote that an argument is optional. Tecnhnically this is just a convention (and probably comes from Unix CLI Usage Messages), but in my experience is so widely used that always interpreting square brackets as denoting an optional argument is usually a safe assumption to make.

romellem
  • 5,792
  • 1
  • 32
  • 64
  • thanks but the comment from Andrey, solved my issue. so it's upvoted. :D – GD- Ganesh Deshmukh Sep 04 '18 at 15:31
  • it seems, to be working. but I am using it in my nodejs app and and If it's not `alphanumeric ` then It should not work, but it worked and registered user and didn't print to console as `Not an alphanumeric`, my github link for code is https://github.com/ganeshdeshmukh1612/connect-geeks/commits/master and main file where I used this logic is here https://github.com/ganeshdeshmukh1612/connect-geeks/commit/fe62bc82a9830bbd8b6b6bfebe59b8e4762b1d26#diff-d832326d3a8f15e682c27e9b3165dcf7 – GD- Ganesh Deshmukh Sep 04 '18 at 15:56
  • or should I add this, in main question? – GD- Ganesh Deshmukh Sep 04 '18 at 15:57
  • 1
    @ganeshdeshmukh that is a different issue. Your original question is essentially "When I call `Validator.isAlphanumeric([(data.password,'en-US')])` I receive an error," and that because you were not calling the `isAlphanumeric` function correctly. Now that you've corrected this implementation error, it looks like the overall functionality you want to achieve isn't working, but that would be a separate issue from your original post. So, I would not edit your post above, and instead ask another question. – romellem Sep 04 '18 at 16:48