0

I have this Regular Expression:

^([^6]*)6 

I want it to match ONLY the first character and make sure its a 6. E.g.

60123456789

It should return 1 match

0123456789 should return 0 matches

How can I do this?

Currently the results it returns in my application is: 601234567890 returns an error

EDIT: This is a data annotation above my ViewModel property

[RegularExpression("^([^6]*)6", ErrorMessage = "Phone Numbers must start with a 6")] 
14
  • 1
    How about ^6? Commented Aug 6, 2019 at 4:39
  • ^6 will cause problems when there are more characters Commented Aug 6, 2019 at 4:40
  • /^6/.test('601234567890') Commented Aug 6, 2019 at 4:40
  • can you post your code along Commented Aug 6, 2019 at 4:41
  • [^6]6 does this work or [^\d]6? Commented Aug 6, 2019 at 4:42

1 Answer 1

0

I think this should work:

'6123'.match(/^6(.+)$/)[0] 

Where, '6123': is any number. For numbers starting with 6 it will match else it would return null.

Let me know if that works.

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry how do I place this in the code in my question? The input is dynamic.
@JianYA, is this a jquery plugin ? It should work if you used the following string : "^6(.+)$".
Its the .net core syntax

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.