0

I am trying do pincode validation using regex which takes only 6 numbers.

I am able to do validation for numbers but it is also accepting letter "e" as well which I want to skip.

Regex code: pincodePattern = /^\d{6}$/;.

thanks.

chandru
  • 169
  • 1
  • 12
  • 4
    *it is also accepting letter "e"* I'm extremely doubtful. Can you post a [MCVE]? – CertainPerformance Aug 22 '18 at 05:35
  • Yeah: 1e10 is allowed in \d - change to [0-9] – mplungjan Aug 22 '18 at 05:39
  • 1
    @mplungjan I'd be very interested if you could reproduce that because `/^\d{6}$/.test('1e10')` produces `false` for me – Phil Aug 22 '18 at 05:43
  • @mplungjan—perhaps you omitted the sarcasm emoji… :-/ – RobG Aug 22 '18 at 05:43
  • 2
    `/^\d{6}$/.test(1e5)` return `true`. `/^\d{6}$/.test("1e5")` return `false` – Neverever Aug 22 '18 at 05:44
  • Because `1e5` is not a **string**. When converted to a string, it is `"100000"` – Phil Aug 22 '18 at 05:44
  • So `.test` coerces numbers to strings, interesting. – CertainPerformance Aug 22 '18 at 05:45
  • 1
    @Phil exactly! there is nothing wrong with the regex, OP needs to check what he has fed into the regex. – Neverever Aug 22 '18 at 05:45
  • 1
    @CertainPerformance—per [*ECMA-262*](http://ecma-international.org/ecma-262/9.0/#sec-regexp.prototype.test): *3. Let string be ? ToString(S).* ;-) – RobG Aug 22 '18 at 05:47
  • @mplungjan i have tried this(`[0-9]`) but still it is accepting letter "e". thanks... – chandru Aug 22 '18 at 05:48
  • @Phil Drat - it sounded plausible. I however think that it is the underlying issue. The coercion to number before he tests – mplungjan Aug 22 '18 at 05:48
  • 1
    @RobG See `3. Let string be ? ToString(S).` - the input apparently does not need to be a string. I was surprised, I would have thought `/^\d{6}$/.test(555555)` would have thrown a `TypeError` or something – CertainPerformance Aug 22 '18 at 05:48
  • @CertainPerformance—internally, 1e10 is a number, that representation is just in the code, the actual value isn't that literally. `1e4.toString()` gives "10000". – RobG Aug 22 '18 at 05:50
  • OP must be taking the string input and running it through `Number(stringInput)`. That's the only way (so far) I've been able to see this happening – Phil Aug 22 '18 at 05:51
  • 1
    http://jsfiddle.net/mplungjan/ep6zqja5/ `var pincodePattern = /^\d{6}$/ var test1 = "3e5" console.log(test1,pincodePattern.test(test1)); console.log(+test1, pincodePattern.test(+test1)); ` – mplungjan Aug 22 '18 at 05:53
  • 1
    PLEASE post the COMPLETE relevant code in a [mcve] with the input field and the event handler. Click the `<>` and create a snippet – mplungjan Aug 22 '18 at 05:55
  • @CertainPerformance—one of the good parts of JS (probably a bad part according to Crockford) is that where the value of an argument to a built–in method must be of a certain type, it is first coerced into that type. The only case where `ToString(value)` will throw a TypeError is where the Type of *value* is Symbol (per [*ToString*](http://ecma-international.org/ecma-262/9.0/#sec-tostring)). – RobG Aug 22 '18 at 09:14

0 Answers0