Advertisement
vencinachev

Dual validation

Apr 21st, 2021
947
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.98 KB | None | 0 0
  1. <!DOCTYPE>
  2. <html>
  3.     <head>
  4.         <title>Web form validation</title>
  5.         <script>
  6.            function checkNumber() {
  7.                let number = parseInt(document.getElementById("num").value);
  8.                if (number >= 1 && number <= 10){
  9.                    return true;
  10.                } else {
  11.                    alert("Invalid data!");
  12.                    document.getElementById("num").value = "";
  13.                    return false;
  14.                }
  15.            }
  16.         </script>
  17.     </head>
  18.     <body>
  19.        <h2>Validate number</h2>
  20.        <form action="#" method="post" onsubmit="return checkNumber()">
  21.           Number:
  22.           <input type="text" name="num" id="num">
  23.           <input type="submit" name="check" value="Check number">
  24.        </form>
  25.        <?php
  26.           if (isset($_POST["check"])){
  27.               $num = $_POST["num"];
  28.               if (is_numeric($num) && $num >= 1 && $num <= 10) {
  29.                   echo "<p id='info' style='color:green;'>Your number ". $num ." is valid!</p>";
  30.               } else {
  31.                   echo "<p id='info' style='color:red;'>Your number ". $num ." is invalid!</p>";
  32.               }
  33.           }
  34.        ?>
  35.     </body>
  36. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement