wmguk Posted August 20, 2007 Share Posted August 20, 2007 Hey, When I create an album, i have to enter an album name - set as $login, what i need to do is allow the whole string, ie. $login = "Demo Album" but I dont know how to allow the whitespace, and also i need to have a little submit button next to where the name is entered that I can click on and it will check if the name has already been used? this then refreshes back to the same page.. any ideas or point me in the right kind of direction please? Quote Link to comment https://forums.phpfreaks.com/topic/65796-validation-checker-and-odd-strings/ Share on other sites More sharing options...
Wuhtzu Posted August 20, 2007 Share Posted August 20, 2007 To validate the name you should use regular expression (regex) <?php $name = "Demo Album"; if(preg_match("/^[a-z]+$/",$name)) { echo "Name is valid"; } else { echo "Name is invalid"; } ?> This is a really good place to know when working with regex: http://www.regular-expressions.info/reference.html The above code example uses PHP's preg_match() function which matches a string against a regular expression. The expression ^[a-z]+$ means the beginning of the string followed by one or more letters in the range a-z followed by the end of the line. You should use something like: "/^([a-z0-9]+)([_\. -]*)([a-z0-9]+)$/i" It says that the string must begin with a-z or 0-9 and can be followed by a space, . (dot), _ (underscore) or - (minus) but must end with a-z or 0-9. And because of the /i it is case insensitive. Thats the validation part Quote Link to comment https://forums.phpfreaks.com/topic/65796-validation-checker-and-odd-strings/#findComment-328730 Share on other sites More sharing options...
wmguk Posted August 20, 2007 Author Share Posted August 20, 2007 ok thats cool ta, the other bit of the script needs to query the database on click and check if its already in use, if it is, ask for a new one, if it isnt then allow it to be used. any thoughts Quote Link to comment https://forums.phpfreaks.com/topic/65796-validation-checker-and-odd-strings/#findComment-328748 Share on other sites More sharing options...
Wuhtzu Posted August 20, 2007 Share Posted August 20, 2007 The query it self should be easy: <?php //This is the part to think about - how to get the album name which the user requests a check for? $album_name = ""; $get_name_count = mysql_query("SELECT COUNT(album_name) FROM album_table WHERE album_name=" . $album_name); $num_of_names = mysql_result($get_name_count,0,0); if($num_of_names > 0) { echo "Name is already used"; } else { echo "Name is available"; } ?> The easiest think would be to perform the name check when the form is submitted. So when the user clicks "Create album" (or what ever the submit button is called) he simply gets an error message if the name is already taken... <?php if(isset($_POST['album_name']) && isset($_POST['something_else']) { perform the name check and display an error if the name already exists } ?> Quote Link to comment https://forums.phpfreaks.com/topic/65796-validation-checker-and-odd-strings/#findComment-328781 Share on other sites More sharing options...
wmguk Posted August 20, 2007 Author Share Posted August 20, 2007 the other way i was thinking was a javascript on click, that pops up a page telling you its ok, or asking to try another.. I have seen something similare - i think it was on eBay where you can check if the username is available before keep trying to submit the form Quote Link to comment https://forums.phpfreaks.com/topic/65796-validation-checker-and-odd-strings/#findComment-329052 Share on other sites More sharing options...
chocopi Posted August 20, 2007 Share Posted August 20, 2007 Thats using Ajax, you can run php without refreshing a page Quote Link to comment https://forums.phpfreaks.com/topic/65796-validation-checker-and-odd-strings/#findComment-329089 Share on other sites More sharing options...
wmguk Posted August 24, 2007 Author Share Posted August 24, 2007 hmm, Im totally lost with Ajax, can anyone point me in the right direction? is there a way so that once you enter the album name, then click the next field it checks if the album name is taken, and displays either a tick or a cross? Quote Link to comment https://forums.phpfreaks.com/topic/65796-validation-checker-and-odd-strings/#findComment-332471 Share on other sites More sharing options...
teng84 Posted August 24, 2007 Share Posted August 24, 2007 if you dont want the page to refresh to validate some input, then ajax is the last option try http://www.w3schools.com/php/php_ajax_database.asp http://www.w3schools.com/php/php_ajax_livesearch.asp that will give you what you exactly need ;D Quote Link to comment https://forums.phpfreaks.com/topic/65796-validation-checker-and-odd-strings/#findComment-332478 Share on other sites More sharing options...
teng84 Posted August 24, 2007 Share Posted August 24, 2007 To validate the name you should use regular expression (regex) <?php $name = "Demo Album"; if(preg_match("/^[a-z]+$/",$name)) { echo "Name is valid"; } else { echo "Name is invalid"; } ?> This is a really good place to know when working with regex: http://www.regular-expressions.info/reference.html The above code example uses PHP's preg_match() function which matches a string against a regular expression. The expression ^[a-z]+$ means the beginning of the string followed by one or more letters in the range a-z followed by the end of the line. You should use something like: "/^([a-z0-9]+)([_\. -]*)([a-z0-9]+)$/i" It says that the string must begin with a-z or 0-9 and can be followed by a space, . (dot), _ (underscore) or - (minus) but must end with a-z or 0-9. And because of the /i it is case insensitive. Thats the validation part this is always seems to be the solution i see here LOL why not use ctype_alpha very simple and cool or ctype functions ......... i guess regex is only for those special chars, emails, etc... that you want to find matches Quote Link to comment https://forums.phpfreaks.com/topic/65796-validation-checker-and-odd-strings/#findComment-332504 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.