designaire Posted March 6, 2008 Share Posted March 6, 2008 I'm trying to validate if a field has too many charaters. The following code works but is there a way that I can check for more than one variable to see if it's the charlimit is over 10? $charlimit = 10; if (strlen($name) <= $charlimit) { echo 'Yes you are ok.';} else { echo ' your field data has too many charaters';} Quote Link to comment Share on other sites More sharing options...
uniflare Posted March 6, 2008 Share Posted March 6, 2008 put it into a function: <?php // Function checklength($vars[,$limit]) // returns true if limit is never reached, returns the string that exceeded size if so. // $vars can be either a string "asdsgasgdfjd" or an array of strings array("agshj","dghyhkyuryteyuy"); function checklength($vars,$limit=10){ if(is_array($vars)){ foreach($vars As $str){ if (strlen($str) <= $limit) { continue; } else{ return $str; } } return true; }else{ if (strlen($str) <= $limit) { return true; } else{ return $str; } } } $string = array( "maximumstr" , "maximum str"); if(checklength($strings,10) == true){ // continue }else{ echo ("'$str'<br /> Exceeded maximum string length"); } // will return: maximum str // Exceeded maximum string length ?> hope this helps, Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.