Jump to content

length checking


verN

Recommended Posts

hi, i wanted to know how to check that the user hasn't inputted a vlaue more then for instance 15 charactrs.

 

I have a form which takes the name, and other contact details. But I would like to preverent the user from entering 30 characters rather then less then 20 is there a way to do this.

Thanks

 

<input type=text name=name size=20 />

 

i was thinking one could use javascript where the input tag can take onchange or can php do this

 

 

Link to comment
https://forums.phpfreaks.com/topic/46684-length-checking/
Share on other sites

<input type=text name=name size=20  onChange="javascript:return check(this.value);"/>

<script language=javascript>
          function ckeck(value){
                 if(value.length > 16){
                       alert('You can not enter more then 15 characters')
                       return false;
                  }
           return true;
           }
</script>

Link to comment
https://forums.phpfreaks.com/topic/46684-length-checking/#findComment-227452
Share on other sites

In PHP you can use strlen to get the length of a string. If your input is part of a form you could use strlen to check the length of your string.

 

For example:

<?php

$myvar = 'this is some text to test';

if (strlen($myvar) >= 15)
{
// Do this if too long
echo "String too long!";
}
else
{
// Do this is fine
echo "String is fine!";
}
?>

 

Edit: Ah, got beaten to it!

Link to comment
https://forums.phpfreaks.com/topic/46684-length-checking/#findComment-227456
Share on other sites

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.