Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Dec 2002
    Posts
    109
    Thanks
    0
    Thanked 0 Times in 0 Posts

    E-Reg The Letter

    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

  • #2
    Registered User
    Join Date
    Feb 2003
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hey,

    try this:

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

  • #3
    Regular Coder
    Join Date
    Oct 2002
    Posts
    380
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It's probably simpler to use preg_match.
    PHP Code:
    $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.

  • #4
    Regular Coder
    Join Date
    Dec 2002
    Posts
    109
    Thanks
    0
    Thanked 0 Times in 0 Posts

    i'm new

    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.

  • #5
    Regular Coder
    Join Date
    Oct 2002
    Posts
    380
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Regular Coder
    Join Date
    Dec 2002
    Posts
    109
    Thanks
    0
    Thanked 0 Times in 0 Posts

    thanks for your patience

    i havent quite got my brain wrapped around it quite yet... but i think i'm getting warm.

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

  • #7
    Regular Coder
    Join Date
    Dec 2002
    Posts
    109
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ya know what i think just about got it...what if i have it like this

    PHP Code:
    <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.

  • #8
    Registered User
    Join Date
    Feb 2003
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Kiwi,

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

    Jesh

  • #9
    Regular Coder
    Join Date
    Oct 2002
    Posts
    380
    Thanks
    0
    Thanked 0 Times in 0 Posts
    \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.

  • #10
    Regular Coder
    Join Date
    Dec 2002
    Posts
    109
    Thanks
    0
    Thanked 0 Times in 0 Posts

    happy happy!

    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •