Jump to content

validation checker and odd strings?


wmguk

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
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 :)

 

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

Link to comment
Share on other sites

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.