/    Sign up×
Community /Pin to ProfileBookmark

Form Validation help

Having some problems with getting this form to validate properly. I was able to get it to noticed if the field was empty. Now i need it to make sure the correct data is entered in ie.. numbers for serials and text for names.
Here is what i have now

[code]
function numchk($ticket){
if (!eregi(“^[a-z]{1,6}”, $ticket)){
return TRUE;
} else {
return False;
}
}

if (!numchk($tt)) {
echo ‘<font color=”red”>Ticket Number must contain numbers only!<br>’;
} else {
if (!empty($_POST[‘ticket’])){
$tt = $_POST[‘ticket’];
} else {
$tt = NULL;
echo ‘<font color=”red”>Enter a Ticket Number<br>’;
}
}
[/code]

This works for the most part my only problem is if it has letters entered in it still completes the form but shows in red lettering “Ticket Number must contains numbers only!”. I thought it would stop the script and how that error. But it finishes the script and shows the error. What am i doing wrong?

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@so_is_thisNov 24.2006 — I don't think you've showed enough code in order to answer that question.

However, try this alteration of your code:
[code=php]
function numchk($ticket)
{
if (preg_match('/^d{6}$/', $ticket))
{
return TRUE;
}
return FALSE;
}

if (numchk($tt))
{
if (empty($_POST['ticket']))
{
$tt = NULL;
echo '<font color="red">Enter a Ticket Number<br>';
} else
{
$tt = $_POST['ticket'];
}
} else
{
echo '<font color="red">Ticket Number must contain 6 digits only!<br>';
}[/code]
×

Success!

Help @prophit spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...