Conditional phone value validation - regex breaks at submission while other coding options crash system.

wm wmelis 15h ago

Dear All, 

Having created a contact form, I added a Thick box that allows people to say they want a call back. When they thick the box then 2 additional fields appear, one asking for a phone number and another is a set of check boxes to indicate a preferred time window. On the phone number, I want to make sure I get a valid number as part of the submission, but seem to be running in circles not getting there. I have screenshots of the "standard setup", but am unable to get them within the file limit independent of file type or quality, soI will talk through what I tried.On the field I had put an input mask, which is normally set to the following: 

mask=00000-000000lazy=falseplaceholderChar=#

Tried so far: 

1) Using Regex, to check for the valid entry, namely "^[0-9]{5}-[0-9]{6}$", I also tried to adjust that and chance the mask in case there would be some characters added that I am not aware of, but that makes no difference, I also remove the input mask completely, but again that had no impact. What happens is that I can enter the form without ticking the box, when I thick the box I enter a phone number and it is valid, then when I press the "send" button the form states that the phone number is not valid and does not want to submit, if I remove a number and add one again the field is happy, I then press "send" and same problem, so I am unable to submit with a valid number because when sending it seems the field becomes invalid. I also tried to change the regex to even the most generic ".*" and the problem persists. I thought this relates to the combination of required and regex, but if I remove the required then it still fails, so it has to do with the regex, not the required behaviour or the input mask. Why it happens/changes at point of pressing the send button is what really puzzles me. When I have the required without the check on the values entered then it allows a partial phone number to be submitted, which is exactly what I want to prevent. 

2) I removed the regex part and tried with valid function behaviour. If I do that, and paste the following php code in the box, then the system crashes on trying to save that. 

function validatePhoneNumber($value) {      $cleaned = preg_replace('/\s+/', '', $value);      if (preg_match('/^[0-9]{11}$/', $cleaned)) {            return true;      }      return 'Please enter a valid 11 digit UK phone number';}

The code is gone on reloading the page. If I simplify the code to this: 

functionvalidatePhoneNumber($value){  return true;}then it saves, but that obviously does not allow me to achieve much. 

3) I also tried to add javascript code to achieve something similar, but have the same problem when I add in the code the system crashes and is gone on reloading.What am I missing here as this should be fairly straight-forward, I thought, until I started hitting all these walls ... your input would be much valued as this is my first chronoform "advanced" experience. 

Thanks, 

Post a Reply