verN Posted April 12, 2007 Share Posted April 12, 2007 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 More sharing options...
Dragen Posted April 12, 2007 Share Posted April 12, 2007 if it's unputting into a database and you have a limit on the database of 30 characters, then I think it should display an error. Link to comment https://forums.phpfreaks.com/topic/46684-length-checking/#findComment-227442 Share on other sites More sharing options...
Orio Posted April 12, 2007 Share Posted April 12, 2007 <?php if(strlen($var) > 15) die("Too long!"); ?> Orio. Link to comment https://forums.phpfreaks.com/topic/46684-length-checking/#findComment-227451 Share on other sites More sharing options...
jitesh Posted April 12, 2007 Share Posted April 12, 2007 <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 More sharing options...
Gibbs Posted April 12, 2007 Share Posted April 12, 2007 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 More sharing options...
Guest prozente Posted April 12, 2007 Share Posted April 12, 2007 jitesh javascript shouldn't be relied on since it's clientside scripting. verN what you're wanting is what Orio posted. Link to comment https://forums.phpfreaks.com/topic/46684-length-checking/#findComment-227482 Share on other sites More sharing options...
clown[NOR] Posted April 12, 2007 Share Posted April 12, 2007 why not just limit the <input> to 15? <input name="textfield" type="text" maxlength="15" /> Link to comment https://forums.phpfreaks.com/topic/46684-length-checking/#findComment-227488 Share on other sites More sharing options...
tauchai83 Posted April 12, 2007 Share Posted April 12, 2007 For good practice, the one posted by Orio is the best way. Link to comment https://forums.phpfreaks.com/topic/46684-length-checking/#findComment-227490 Share on other sites More sharing options...
Guest prozente Posted April 12, 2007 Share Posted April 12, 2007 link=topic=135671.msg572588#msg572588 date=1176373910] why not just limit the <input> to 15? <input name="textfield" type="text" maxlength="15" /> clown[NOT], this can be changed by the user so it can't be relied on. Link to comment https://forums.phpfreaks.com/topic/46684-length-checking/#findComment-227491 Share on other sites More sharing options...
clown[NOR] Posted April 12, 2007 Share Posted April 12, 2007 oh ok... didnt know that... thanks for letting me know =) Link to comment https://forums.phpfreaks.com/topic/46684-length-checking/#findComment-227798 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.