Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/65796-validation-checker-and-odd-strings/
Share on other sites

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 :)

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
}
?>

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

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 ;D ;D

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.