andr-04 / inputmask-multi

http://andr-04.github.com/inputmask-multi/
253 stars 106 forks source link

How to validate field with various mask length ? #48

Open AntonGrekov opened 4 years ago

AntonGrekov commented 4 years ago

I have met an issue with validation phone field that could be different length. Being not very experienced with validations i have found may be not best solution but for my situation it worked you. May be it will help you.

So problem is that for one country mask length - 10, for another 12, for another 13 and we need to warn user to fill phone field completly. I used this algorithm:

  1. Change inputmask initialization setting 'autoUnmask': false, so when getting input value - you get it with _ symbol
  2. Use this simpliest regexp to find if there any '_' left in input field , if so - field not completed

if (/_/.test(phoneInput.val())) { alert('phone empy, pls enter phone'); //do any stuff u want here - shows divs erros etc }

Little advantage over //clearIncomplete: true, is that it leaves user input to complete later

Would be glad to know correct/other ways to validate different length