0

For example, if a user enter alphabet in a 'Phone' input field and I want to display a line of message 'Only numeric number is allow' under the input field. How can I do this?

I tried:

<div> <label>Cell Phone No</label> <input type="text" name="cellphnumber"  value="" class="form-control"> </div>
<?php if (!preg_match ("/^[0-9]*$/", $cellphnumber) ) {  
            echo <p>"Only numberic value is allowed."</p>
        }?>

AND

<div> <label>Cell Phone No</label> <input type="text" name="cellphnumber"  value="" class="form-control"> </div>
<script>
if (!preg_match ("/^[0-9]*$/", $cellphnumber) ) {  
    <a>Only numeric value is allowed.</a>;
        }
</script>
LJ27
  • 27
  • 4
  • Neither of those wait until the user has actually typed anything. They also don't attempt to read from the field (just from some undefined variable). – ADyson May 10 '21 at 08:08
  • But you could just write `input type="number"` in the HTML and that would prevent any non-numeric characters. You should still have some server side validation of the submitted value in PHP but it needs to happen after the data has been submitted! – ADyson May 10 '21 at 08:09

0 Answers0