• Hi,

    I’m trying to figure out how to implement conditional logic in a form without using a plugin. Specifically, I’m trying to add functionality that will check the fidelity of a phone number entered into a textfield by the user (e.g. to check starting digits, number of characters etc).

    I’ve been going through the PHP 5 Form Validation examples on w3schools.com but the functionality in these examples is not developed for WordPress.

    I’m new to WordPress development and am looking for advice on where to start.

    Any help would be much appreciated.

    Cheers,
    Tony

Viewing 3 replies - 1 through 3 (of 3 total)
  • It’s HTML5, not PHP 5.

    If the browser supports HTML5, all you need to do is use the correct type and pattern on the input. If it doesn’t, then it’s not checked. But you should always check on the server. Client-side checking can be interfered with.

    https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/tel

    but yeah, if the browser doesn’t support this, it will fall back to a regular text input.

    Moderator bcworkz

    (@bcworkz)

    You would use JavaScript to validate client side. Strip out non-numeric chars and count the remaining digits. The chars we use to separate number segments are locale specific and meaningless to phone systems. But the digit count is also locale specific, plus certain digits could be optional. For example, country codes are optional within the country, as sometimes are area or region codes. So your digit count should accept a range of results, or be able to recognize optional portions.

    How you get JS onto a page in WP is unique to WP, but the actual JS is not any different than any other web page. Like Joy said, you must do the same check server side regardless of whether it’s checked client side or not. The client side check is optional and mainly serves to make a better UX.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Implementing conditional logic in form without using plugin’ is closed to new replies.