How can PHP be used to restrict user input in a <input type='text'> field to only numbers?

To restrict user input in a <input type='text'> field to only numbers, you can use PHP to validate the input and allow only numeric characters to be entered. This can be achieved by using a combination of regular expressions and PHP functions to check if the input contains only numbers. By implementing this validation on the server-side, you can ensure that only valid numeric data is submitted.

&lt;?php
if(isset($_POST[&#039;submit&#039;])){
    $input = $_POST[&#039;number_input&#039;];
    
    if(preg_match(&#039;/^\d+$/&#039;, $input)){
        // Input contains only numbers
        echo &quot;Input is valid: &quot; . $input;
    } else {
        // Input contains non-numeric characters
        echo &quot;Invalid input. Please enter only numbers.&quot;;
    }
}
?&gt;

&lt;form method=&quot;post&quot;&gt;
    &lt;input type=&quot;text&quot; name=&quot;number_input&quot;&gt;
    &lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot;&gt;
&lt;/form&gt;