Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regexp help needed!!!

Status
Not open for further replies.

fowlerlfc

MIS
Mar 20, 2002
136
US
I'm very new to PHP. I am currently trying to validate an html form before I pass the data on to a database. I want to make sure the data is in certain formats. For instance, I want the data in the variable $name to be a firstname followed by a space followed by a lastname. Can you use a regular expression to check data held by a variable? If so, could someone help me with the example listed above?

Thanks in advance for all your help!
 
something like :
if (!ereg("^[a-zA-Z]*[[:space:]][a-zA-Z]$",$field_to_validate){
echo "not a valid name";
}else{
echo "validated";
$name=explode(" ",$field_to_validate);
echo &quot;First name: $name[0] <br> Last Name: $name[1]&quot;;
}

// I've been on wobbly juice for 5 hours so this may not be 100% accurate, see fuction manual and search for ereg, preg_match , eregi and explode to check if I've done this right.

Ty
Karv ***************************************
Party on, dudes!
[cannon]
 
Personally, I prefer PCREs (Perl Compatible Regular Expressions). They are more powerful than any POSIX regular expression and in my opinion...much easier to read & learn.

Example:

$text = &quot;I sell pots plants pistachios, pianos and parrots&quot;;

if( preg_match_all(&quot;/\bp\w+s\b/&quot;,$text,$array) ) {
for( $x=0;$x<count($array);$x++ ) {
for( $y=0;$y<count($array[$x]);$y++ ) {
print &quot;\$array[$x][$y]: &quot;.$array[$x][$y].&quot;<br>\n&quot;;
}
}

print &quot;<br>\n&quot;;
}

$names = array(&quot;SomeJoe&quot;,&quot;Some Joe&quot;,&quot;No No&quot;,&quot;Yes Yes&quot;);

print &quot;The following names are valid:<br>\n&quot;;

foreach( $names as $n ) {
if( preg_match(&quot;/\b\w\w\w+\b\s\b\w\w\w+\b/&quot;,$n,$array) ) {
$name = explode(&quot; &quot;,$n);
print $name[0].&quot; &quot;.$name[1].&quot;<br>\n&quot;;
}
} - pain4u
 
if (preg_match(&quot;/^\S+\s\S+$/&quot;,$_POST[var]) {
#code to run on succes
} else {
#code to run on failure
} --BB
 
I'm about to go insane! Here's my script:

<?
if (!ereg(&quot;^[a-zA-Z]&quot;, $name) echo &quot;Improper format&quot;;
?>

I have run this with alpha and numeric characters in $name, but only get a whitescreen as output. I've tried all the suggestions above, and I only get a whitescreen as output.

I am using php 4.20. Is there maybe something wrong with my configuration (php.ini)?
 
<?
if (!ereg(&quot;^[a-zA-Z]&quot;, $name) {
echo &quot;Improper format&quot;;
}
?> ***************************************
Party on, dudes!
[cannon]
 
I doubt it has anything to do with your php.ini. It's hard to tell what's wrong since I'm not the one writing it. None of the example's work for you?? That's odd...what name are you using? - pain4u
 
actually....your both missing the last &quot;)&quot;

if( !ereg(&quot;^[a-zA-Z]&quot;,$name) )
echo &quot;Improper format&quot;; - pain4u
 
OK lets troubleshoot.
<?php
if(!isset($name)){
echo &quot;No name entered&quot;;
}else{
if (!ereg(&quot;^[a-zA-Z]&quot;, $name)) {
echo &quot;Improper format&quot;;
}else{
echo &quot;you entered $name&quot;;
}
}
?> ***************************************
Party on, dudes!
[cannon]
 
He I figured the last ) as I was writing the post above, still it may prove useful to verify that a name has been entered as well :) ***************************************
Party on, dudes!
[cannon]
 
hehe, good thinking KarveR! :)

This site/forum needs a re-make. We can't edit our own post's or send private messages. Those 2 features would be nice to have. ;) - pain4u
 
Boy do I feel stupid! I was missing those parenthesis, and that's what was causing the problem. Thanks for taking the time to help an extreme newbie!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top