Jump to content

validate length of charaters


designaire

Recommended Posts

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';}

Link to comment
https://forums.phpfreaks.com/topic/94765-validate-length-of-charaters/
Share on other sites

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,

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.