Ugluth Posted December 20, 2010 Share Posted December 20, 2010 Hello I want to check that a variable the user enters is not empty and that it is a string (no numbers). Here is what i have written: <?php include('css/layout.css.php'); include('css/menu.css'); if (isset($_POST['submit'])) { include('includes/dbconn.php'); if (is_string($_POST['name']) && strlen($_POST['name']) > 0) { $name = mysql_real_escape_string($_POST['name']); } else { echo "Category name must be a word!<br />"; } if (isset($name)) { echo $name; } else { echo "something"; } } ?> and this is the form i'm getting the data from: <form action="category_add.php" method="post"> <table class="table-view"> <tr> <td><label for="name">Category Name</label></td> <td><input type="text" name="name" id="name"></td> </tr> <tr> <td><input type="submit" value="Save changes" name="submit" id="submit"></td> </tr> </table> </form> If i enter nothing it works fine, if i enter a string it also works fine, the problem is when i enter 123 in the textbox, i dont get the "Category name must be a word!" as i expected i would. Could someone help me with this one please? Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/222223-is_string-not-working-as-i-expected/ Share on other sites More sharing options...
BlueSkyIS Posted December 20, 2010 Share Posted December 20, 2010 is_string() will not tell you whether all characters are letters. is_string() only tells you whether what you passed to it is a string. a string can include numbers. for example, this is a string: "hello 123 world"; If you want to tell whether there are any numbers, I would use preg_replace like this: if (!empty($_POST['name'])) && strlen(preg_replace("/[\D]/","",$_POST['name'])) == 0) { // $_POST['name'] contains no numbers } Link to comment https://forums.phpfreaks.com/topic/222223-is_string-not-working-as-i-expected/#findComment-1149570 Share on other sites More sharing options...
AbraCadaver Posted December 20, 2010 Share Posted December 20, 2010 http://us2.php.net/manual/en/function.ctype-alpha.php Link to comment https://forums.phpfreaks.com/topic/222223-is_string-not-working-as-i-expected/#findComment-1149592 Share on other sites More sharing options...
Ugluth Posted December 20, 2010 Author Share Posted December 20, 2010 Thank you very much for your replies, you cleared things up for me Link to comment https://forums.phpfreaks.com/topic/222223-is_string-not-working-as-i-expected/#findComment-1149596 Share on other sites More sharing options...
Pikachu2000 Posted December 20, 2010 Share Posted December 20, 2010 IIRC, all form data is sent as string type, so is_string() will return true for any form field validation. Link to comment https://forums.phpfreaks.com/topic/222223-is_string-not-working-as-i-expected/#findComment-1149598 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.