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
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,

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.