Announcement

Collapse
No announcement yet.

PHP Validation of length of text input

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • PHP Validation of length of text input

    How can i do a validation of identity number (thats needs to be 13 digits long) and then use the first four numbers to check if its a pensioner.(after clicking submit).
    I have done a check for null and that currently worksOnly using html and php

    Code:
    $identity="";$errId=0;
    
    if(isset($_POST["submit"]))
    {
    if($_POST["identity"]==null)
    $errId=1;
    else
    $identity= $_POST["identity"];
    }
    <body>
    Code:
    SA ID Number<input type="text" name="identity" value="<?php echo $identity ?>" id="identity"/>
    <?php if ($errId) echo "Please enter an ID number" ?>
    Code:
    <input type="submit" value="getQuote" name="submit"/>

  • #2
    There are a couple of ways to do that. one you can check is its a number by using is_numeric() and/or you can use regular expressions to check if a number. Then you can use strlen() to takeoff the first 4 numbers.

    I would also check on your submit for the value as you expect one, not just isset, which can also be true if 0

    PHP Code:
    if(!empty($_POST["submit"]) and $_POST['submit'] == "getQuote")
    {
    if(empty(
    $_POST["identity"]))
    $errId=1;
    else
    $identity$_POST["identity"];

    Have a Script or Snippet you want to share?

    WWW Standards: HTML 4.01,
    HTML 5, CSS2.1, CSS3, XHTML 1.0
    PHP Standards: PHP Standards

    Comment

    Working...
    X