1
jilong
check field length function
  • 2007/7/18 8:56

  • jilong

  • Just popping in

  • Posts: 61

  • Since: 2007/1/10


I had do some hack on edituser.php and function.php to do a validation for some fields. like Full Name field will only allow to enter alphabet and phone number only allow numbering. this two function is working fine with the code below.

function checkname($name)
{
    if (!
$name || !preg_match("/^[a-z @]+$/i",$name)){
        return 
false;
    }
    return 
$name;
}

function 
checkphone($tel_hp)
{
    if (!
$tel_hp || !preg_match("/^[0-9-]+$/i",$tel_hp)){
        return 
false;
    }
    return 
$tel_hp;
}


Now i would like to add in a field length validation for postcode,
Example: if the post code field enter more than 5 or less than 5 digit, the error message will show out.

But i have no ideal how to check the length, below is the code i try and actually not work. I hope to get any one have experience or ideal to do the length validation.

function checkpost($post)
{
for (
$i=0$i<strlen($post); $i++) {
    if (
$post.charAt(i) < 5){
        return 
false;
    }
}
    return 
$post;
}

2
jilong
Re: check field length function
  • 2007/7/19 7:58

  • jilong

  • Just popping in

  • Posts: 61

  • Since: 2007/1/10


Is there any ideal i can refer to?

3
jilong
Re: check field length function
  • 2007/7/19 9:17

  • jilong

  • Just popping in

  • Posts: 61

  • Since: 2007/1/10


OK, finally i solve it mysalve, and like to share out

if (!post || !preg_match("/^d{5}$/i",$post)){
        return 
false;
    }

Login

Who's Online

156 user(s) are online (101 user(s) are browsing Support Forums)


Members: 0


Guests: 156


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Apr 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits