lc21 Posted July 14, 2007 Share Posted July 14, 2007 I have managed to create a script in javascript to ensure that the user can only enter a certain amount of characters in a text area. If users have javascript turned off how can I check for this in PHP? Thank you. Link to comment https://forums.phpfreaks.com/topic/59918-php-validation/ Share on other sites More sharing options...
GingerRobot Posted July 14, 2007 Share Posted July 14, 2007 Use the strlen() function. Link to comment https://forums.phpfreaks.com/topic/59918-php-validation/#findComment-297969 Share on other sites More sharing options...
lc21 Posted July 14, 2007 Author Share Posted July 14, 2007 Thanks, how do I get the string stored in a variable though as the text area just has a name can I use that name and set it to a variable then use the strlen function? Link to comment https://forums.phpfreaks.com/topic/59918-php-validation/#findComment-297981 Share on other sites More sharing options...
GingerRobot Posted July 14, 2007 Share Posted July 14, 2007 Yep. If your method is post and the name of the text area was textarea: <?php if(strlen($_POST['textarea']) > 100){ //too long }else{ //length ok } ?> Link to comment https://forums.phpfreaks.com/topic/59918-php-validation/#findComment-297986 Share on other sites More sharing options...
182x Posted July 14, 2007 Share Posted July 14, 2007 Good tip could come in handy. Link to comment https://forums.phpfreaks.com/topic/59918-php-validation/#findComment-297987 Share on other sites More sharing options...
lc21 Posted July 14, 2007 Author Share Posted July 14, 2007 When the data from a text area is stored in a database does it loose all the white space or when outputting it again is there a way to retain this? Link to comment https://forums.phpfreaks.com/topic/59918-php-validation/#findComment-297997 Share on other sites More sharing options...
drewbee Posted July 14, 2007 Share Posted July 14, 2007 I always recomend doing a trim() of the variable to clean off any additional whitespace that may exist. <?php if (strlen(trim($_POST['myVar'])) > 0) { // Do it!! } else { // Please enter a value } ?> Link to comment https://forums.phpfreaks.com/topic/59918-php-validation/#findComment-298001 Share on other sites More sharing options...
GingerRobot Posted July 15, 2007 Share Posted July 15, 2007 When the data from a text area is stored in a database does it loose all the white space or when outputting it again is there a way to retain this? Use the nl2br() function when you output data that came from a text box. It converts all new lines(\n) to html <br /> tags. Link to comment https://forums.phpfreaks.com/topic/59918-php-validation/#findComment-298668 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.