Home > Blockchain >  Validating client-side data input using a pattern
Validating client-side data input using a pattern

Time:12-25

I am currently working on a project whereby data can be added into a database via a website. Currently I have managed to configure it so that the form accepts title, postcode, vehicle reg and ID number.

Javascript validation is working fine for these entries, with the exception of ID number. All ID numbers are a specific format (2 numbers followed by a . followed by 4 numbers).

I cannot seem to work out how to define the pattern.

Due to the size of my code, I have not posted the full code here (all is validating except this ID validation), but I've provided a snip of the 'if' statement below which I'm trying to come up with.

if (inputElement.id == "wid") {
    pattern = /^[a-zA-Z0-9 ]*$/;
    feedback = "Only 2 numbers followed by a . followed by 4 numbers are 
permitted";

I know that the pattern isn't correct here but I have searched for hours trying to locate some easy to explain guidance and cannot find anything which appears to be relevant.

Any thoughts would be appreciated.

Thank you

CodePudding user response:

You can try out something like https://regex101.com/ to test you regexes, and see an explanation of it.

I think your pattern should be this: /^[0-9]{2}\.[0-9]{4}$/. The first part ([0-9]{2}) makes sure that the id starts with 2 digits, then a dot \. (which must be escaped, because it means "every character" otherwise) and then 4 digits [0-9]{4}

  • Related