PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 307,124 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,003 people online right now. Registration is fast and FREE... Join Now!




form validation

 

form validation, how do i vaidate to only allow letter and numbers and be between 6 and

chris_s_24

3 Nov, 2009 - 11:37 AM
Post #1

New D.I.C Head
*

Joined: 1 Nov, 2009
Posts: 11

i want to vaidate the user input of a form
to only allow the username feild to contain
letters both upper and lower case
and numbers
and between 6 and 12 characters

this is what i got so far
CODE

if(trim($_POST[username])=='' || strlen(trim($_POST[username])) < 6 || strlen(trim($_POST[username])) >12)
{
    // Reshow the form with an error
$reg_error2 = "Your username must be at least 6 characters in length!<br />";
include 'index.php';
exit;  
}


ive been told theres some programs that can do the form validation for me. i did find and try it but it didnt satisfy all my requirements.

can any one recomend any that could do what i am asking. i dont mind paying if it is a decent one



User is offlineProfile CardPM
+Quote Post


RPGonzo

RE: Form Validation

3 Nov, 2009 - 12:16 PM
Post #2

// Note to self: hmphh .... I forgot
Group Icon

Joined: 16 Mar, 2009
Posts: 771



Thanked: 92 times
Dream Kudos: 25
My Contributions
Look into regex validations ...

Look here for some common ones

quick example from that page ...

CODE

$username = trim($_POST['username']);
if (preg_match('/^[a-z\d_]{6,12}$/i', $username)) {
    echo "Your username is ok.";
} else {
    echo "Your username must be at least 6 characters in length!<br />";
}


that would only allow a-z,A-Z,underscores(_),0-9 and a minimum of 6 characters max of 12, again that almost copied and pasted form the site... if you dont want numbers or underscores try this

CODE

$username = trim($_POST['username']);
if (preg_match('/^[a-z]{6,12}$/i', $username)) {
    echo "Your username is ok.";
} else {
    echo "Your username must be at least 6 characters in length!<br />";
}


This post has been edited by RPGonzo: 3 Nov, 2009 - 12:24 PM
User is offlineProfile CardPM
+Quote Post

jaql

RE: Form Validation

3 Nov, 2009 - 12:23 PM
Post #3

D.I.C Head
**

Joined: 19 Oct, 2009
Posts: 59



Thanked: 5 times
My Contributions
CODE

$pattern  = '/^(?=.*[a-zA-Z]+.*)[0-9a-zA-Z]{6,12}$/';
$username = 'awesome123';

if (!preg_match($pattern, $username)) {
  // ..error code..
}
else {
  // ..success code..
}

User is offlineProfile CardPM
+Quote Post

tivrfoa

RE: Form Validation

3 Nov, 2009 - 12:29 PM
Post #4

D.I.C Head
Group Icon

Joined: 25 Jan, 2009
Posts: 93



Thanked: 6 times
Dream Kudos: 125
My Contributions
CODE
<?php

    $users = array();
    $users[0] = "few";
    $users[1] = "tooMuchCharacters";
    $users[2] = "&ops->xD";
    $users[3] = "tivrfoa";
    $users[4] = "chris_s_24";
    $users[5] = "12345";
    $users[6] = "123456";
    $users[7] = "1234567";
    $users[8] = "12345678912";
    $users[9] = "123456789123";
    $users[10] = "1234576891234";
    
    $pattern = "/^\w{6,12}$/i"; // match valid usernames. It also accepts underscores.
    
    foreach($users as $user) {
        if(preg_match($pattern, $user))
            echo "ok. " . $user . " is a valid username<br/>";
        else
            echo "ops. Invalid username: " . $user . "<br/>";
    }

?>

User is online!Profile CardPM
+Quote Post

chris_s_24

RE: Form Validation

3 Nov, 2009 - 01:38 PM
Post #5

New D.I.C Head
*

Joined: 1 Nov, 2009
Posts: 11

thx for the link
an example in the link gave me a starting point

CODE
(preg_match('/^[a-z\d_]{6,12}$/i', $username))

this seems to be something more like i require but this is very confusing and trying to disect

just been having a read through some tutorials
^ doesnt this mean the string being searched must start with this pattern. Do i need this for what im trying to achieve?
$ at the end of a regular expression indicates that the string being searched must end with this pattern. Do i need this for what im trying to achieve?
\d think this is short way of writing 0-9 sm i right?
the / im guessin this is where string starts and ends am i right?
{6,12} think this is self explanatory {min legth,max length}
[] this groups the pattern
can anyone tell me what the "i" was in the above code

this seems to be basic principle
CODE
preg_match('pattern', text_to_search);



so heres what i came up with can anyone tell me if this is correct to what i need?
CODE
preg_match('/[a-zA-Z\d]{6,12}/', ($_POST[username]));





User is offlineProfile CardPM
+Quote Post

RPGonzo

RE: Form Validation

3 Nov, 2009 - 01:52 PM
Post #6

// Note to self: hmphh .... I forgot
Group Icon

Joined: 16 Mar, 2009
Posts: 771



Thanked: 92 times
Dream Kudos: 25
My Contributions
the i you asked about makes the search case insensitive ... meaning you can use a-z and it check for lower and upper case ..

correct on d it stands for digits ( in a basic sense )

Im not the best with regex so i use ALL the tools i can find ... like this one

Regex test Tool

Or like tiv did which IS a good idea .. is to manually test it yourself .. make a simple array with different values some right some wrong of different variations and try it!

This post has been edited by RPGonzo: 3 Nov, 2009 - 01:55 PM
User is offlineProfile CardPM
+Quote Post

chris_s_24

RE: Form Validation

3 Nov, 2009 - 02:10 PM
Post #7

New D.I.C Head
*

Joined: 1 Nov, 2009
Posts: 11

ok so I take out the A-Z and put back in the i
my code now looks like this
CODE
preg_match('/[a-z\d]{6,12}/i', ($_POST[username]));


can anyone tell me what the following are and explain more about as seen in the below code
^
$
i why does it come after the / and not before?
CODE
(preg_match('/^[a-z\d_]{6,12}$/i', $username))


yeah tivrfoa looked complicated when i first read it but looking at it again does seem like a good way of at least testing it thx tivrfoa
User is offlineProfile CardPM
+Quote Post

RPGonzo

RE: Form Validation

3 Nov, 2009 - 02:29 PM
Post #8

// Note to self: hmphh .... I forgot
Group Icon

Joined: 16 Mar, 2009
Posts: 771



Thanked: 92 times
Dream Kudos: 25
My Contributions
the pattern between the // ( slashes ) is the actual pattern

all the modifiers follow the last delimiter to set more options over how the pattern works

more info on modifiers

as to the ^ and $ ... i can honestly say i don't know lol hopefully someone else can chime in on that

more information on ^ and $ ( if you can understand it lol )

This post has been edited by RPGonzo: 3 Nov, 2009 - 02:41 PM
User is offlineProfile CardPM
+Quote Post

chris_s_24

RE: Form Validation

3 Nov, 2009 - 02:39 PM
Post #9

New D.I.C Head
*

Joined: 1 Nov, 2009
Posts: 11

thx RPGonzo

you been a great help

you links have been great
User is offlineProfile CardPM
+Quote Post

tivrfoa

RE: Form Validation

3 Nov, 2009 - 04:00 PM
Post #10

D.I.C Head
Group Icon

Joined: 25 Jan, 2009
Posts: 93



Thanked: 6 times
Dream Kudos: 125
My Contributions
^ and $ are called anchors

^ start of string; start of line
$ end of string; end of line

http://www.regular-expressions.info/anchors.html
User is online!Profile CardPM
+Quote Post

chtombleson

RE: Form Validation

3 Nov, 2009 - 04:25 PM
Post #11

New D.I.C Head
*

Joined: 28 Sep, 2009
Posts: 11



Thanked: 1 times
My Contributions
you can use regex like this:

CODE

if(!$input == eregi([a-z]{character limit}, $input))
{
//error message
}
//or
if(!$input == ereg([a-zA-Z]{character limit}, $input))
{
//error message
}

User is offlineProfile CardPM
+Quote Post

RPGonzo

RE: Form Validation

4 Nov, 2009 - 06:02 AM
Post #12

// Note to self: hmphh .... I forgot
Group Icon

Joined: 16 Mar, 2009
Posts: 771



Thanked: 92 times
Dream Kudos: 25
My Contributions
eregi is deprecated and should not be used ...

http://php.net/manual/en/function.eregi.php
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 02:06PM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month