...

View Full Version : E-Reg The Letter



Shecky
02-06-2003, 06:12 AM
i have 15 short string input fields on a form. Upon submit i want to run one ereg or eregi on all of them to check to make sure they are only letters and numbers, no spaces or bizarro characters. Also each field is not mandatory.

i think i've seen input arrays used before for something like this... i'm new, so i can really get the hang of it.

also, if anyone can help me out with it, after this
(IF !ereg ---> display an error msg)
in the ELSE part i need to prepare a variable to be added to mysql that would be the combined values of all 15 strings.

any pointers?

AIM: t3knosquid
email : produckt_exodus@hotmail.com

Jesh
02-06-2003, 08:43 AM
hey,

try this:


function exclude_valid_char($str) {
$from = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$to = ' ';
return strtr($str, $from, $to);
}

$excluded_valid_chars = exclude_valid_char($string_to_check);
$excluded_valid_chars = ereg_replace (" ", "", $excluded_valid_chars);

if ($excluded_valid_chars != ""){
echo ("Please enter a valid string. Only letters and numbers are allowed.");
exit;
}



This method can be used to find any "bulk" amound of characters.

Just remember, for every character you want to check, make sure there is an equivalent space in the $to string.

Your script will stop executing if characters other than letters or numbers are found...

Remember to replace $string_to_check with the string that you actually want to check.

Jesh

Kiwi
02-06-2003, 10:41 AM
It's probably simpler to use preg_match.


$theerror = 0;
if (preg_match("/\w",$thestring))
{ $theerror = $theerror + 0; }
else
{ $theerror = $theerror +1; }
if ($theerror > 0)
{ $theerror strings are bad }
else
{ all is good }

If your form only contains these fields you want to test, then you can simple parse the entire array with the regex (using foreach, for example) and set and error flag if there's a mis-match. If there are no errors, implode the array which will concatonate all the strings for you.

Shecky
02-06-2003, 11:34 AM
would you mind taking a moment to discrible exactly what /w means in the above expression? I realize it probaly has something to do with the letters i want to match... i was looking up preg_match on php.net and i find the syntax to be insanely complex.

what needs to go there so i can send the user an error message when they've entered non-alphanumeric characters.

I told you, i'm new.

Kiwi
02-06-2003, 12:48 PM
It's a special character in perl regex that means any alpha-numeric character, including underscore. It's equivalent to [A-Za-z0-9_]

Regex isn't the easiest thing to get your head around, but they are worth the time to try and understand them.

In the example I gave, $theerror will be set to 0 if there are no errors, and 1+ if there are errors. You then just test the value of $theerror. If it's not zero, you know something has gone wrong. A more detailed approach could tell you which field was illegal. You'd have to have an error flag for each field.

Shecky
02-06-2003, 01:26 PM
i havent quite got my brain wrapped around it quite yet... but i think i'm getting warm.




<input name="f_word[1]" type="text" size="9" maxlength="18">
<input name="f_word[2]" type="text" size="9" maxlength="18">



this is how i'm entering my each word field.

I'm interested in this preg_replace thing... do you suppose you could tell me how to, instead of displaying an error, simply search the array and replace each invalid (non-"/w") character with an empty a "" ?

I think thats what the other poster on this thread was trying to show me but.. you know.. I was dropped on my head as a child.

A side question, then... would this array be held in memory to resemble something like:

"this,that,and,the,other,thing" ?

I plan to enter this into mysql as a blob, and i'm reasonably sure i want those commas there for later use.

To answer yer question, yes... i am really this stupid.

-shecky

Shecky
02-06-2003, 01:58 PM
ya know what i think just about got it...what if i have it like this



<input name="f_words[]" type="text" size="9" maxlength="18">
<input name="f_words[]" type="text" size="9" maxlength="18">

// up in the form collect the array like that?

$pattern = "whatever pattern i can use to spec invalid chars";
$replacement = "";
preg_replace($pattern, $replacement, $f_words);




maybe theres a shortcut to that pattern, that specifies NOT "/w", but i wouldnt really know that.

i'm sure i have a huge flaw in my logic there somewhere :) thanks for yer help man.

Jesh
02-06-2003, 02:35 PM
Kiwi,

I think your way is much better for alpha numeric testing.

Jesh

Kiwi
02-06-2003, 03:39 PM
\W is any non-alpha-numeric character. If you just want to strip invalid characters, then use:
preg_replace("/\W+/",$thestring,""); This could result in null strings, if there are no valid characters.

The original regex I gave you should have been /\w+/ to match one or more alpha-numeric character.

Shecky
02-06-2003, 06:07 PM
Originally posted by Kiwi
\W is any non-alpha-numeric character. If you just want to strip invalid characters, then use:
preg_replace("/\W+/","",$thestring); This could result in null strings, if there are no valid characters.

The original regex I gave you should have been /\w+/ to match one or more alpha-numeric character.

Not to bug you guys anymore, but i just had to say thanks... i accomplished my goal, and i learned a lot today. Example: PHP is not as programmer-friendly as I first believed :)

Thanks again guys... I'm sure I'll be back! :cool:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum