preg_match trouble (Looks correct, but not matching correctly)

Discussion in 'PHP' started by tlshaheen, Feb 10, 2010.

  1. #1
    I'm currently using the following code to test if a First Name entry in a field has entered text that contains only letters and spaces (no numbers or symbols).

    For the most part, it works. However, some symbols it takes a MATCH. #, &, -, + all are detected as matches and it doesn't give an error. In addition, when I type %, it gives me an error (this could be in reference to other code, so dont kill yourself over it).
    Whats wrong with my preg_match that it allows #,& and other symbols sometimes?

     	 if(!preg_match('/^[-a-z\x20]+$/i', stripslashes($q))) {
      		//echo error msg
    		} 
    Code (markup):

     
    tlshaheen, Feb 10, 2010 IP
  2. thorie

    thorie Peon

    Messages:
    35
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What does var_dump($q) give you before you run that expression in a case where the result is not what you wanted?

    By the way: var_dump(!preg_match('/^[-a-z\x20]+$/i', '#')); returns true. So you'll have to show me $q because what you're saying isn't happening for me.
     
    Last edited: Feb 10, 2010
    thorie, Feb 10, 2010 IP
  3. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #3
    Hi,

    Try this pattern:
    
    if(!preg_match('/^[a-z]+\s?/i', stripslashes($q))) {
      		//echo error msg
    		}
    
    PHP:
    This will test first only name.
    Regards
    Edit: To support large Unicode ranges (ie: [\x{E000}-\x{FFFD}] or \x{10FFFFF}) you must use the modifier '/u' at the end of your expression.
     
    Last edited: Feb 10, 2010
    koko5, Feb 10, 2010 IP
  4. Brandon.Add.On

    Brandon.Add.On Peon

    Messages:
    178
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Your problem could possibly lie in the extra dash before the letter a.
     
    Brandon.Add.On, Feb 11, 2010 IP
  5. tlshaheen

    tlshaheen Peon

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    After outputting $q while I type, I discovered that when I type the symbols in question, it doesn't register them into $q, hence why $q is still correct. (i.e. I type abcd, $q=abcd. I type abcd##, $q still = abcd.) Not sure if its my Ajax code, or simply the way I detect that a letter has been interested into the form. Here is my javascript file, php file, and the form code used. Thanks for the help guys!

    // JavaScript Document
    var xmlhttp;
    
    function checkAlpha(str , id)
    {
    if (str.length==0)
      {
    	  //Check ID, output to correct Document ID
    	  
    	  if (id=="Fname") {
    	document.getElementById("alpha_check_fname").innerHTML="";
    	  }
    	  if (id=="Lname") {
    	document.getElementById("alpha_check_lname").innerHTML="";
    	  }
    	  if (id=="City") {
    	document.getElementById("alpha_check_city").innerHTML="";
    	  }
      	return;
      }
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
      {
      alert ("Your browser does not support XMLHTTP!");
      return;
      }
    var url="../users/form_checks/alpha.php";
    url=url+"?q="+str;
    url=url+"&sid="+Math.random();
    	  if (id=="Fname") {
    		xmlhttp.onreadystatechange=stateChangedFname;
    	  }
    	  if (id=="Lname") {
    		xmlhttp.onreadystatechange=stateChangedLname;
    	  }
    	  if (id=="City") {
    		xmlhttp.onreadystatechange=stateChangedCity;
    	  }
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
    }
    
    function stateChangedFname()
    {
    if (xmlhttp.readyState==4)
      {
      document.getElementById("alpha_check_fname").innerHTML=xmlhttp.responseText;
      }
    }
    
    function stateChangedLname()
    {
    if (xmlhttp.readyState==4)
      {
      document.getElementById("alpha_check_lname").innerHTML=xmlhttp.responseText;
      }
    }
    
    function stateChangedCity()
    {
    if (xmlhttp.readyState==4)
      {
      document.getElementById("alpha_check_city").innerHTML=xmlhttp.responseText;
      }
    }
    
    function GetXmlHttpObject()
    {
    if (window.XMLHttpRequest)
      {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      return new XMLHttpRequest();
      }
    if (window.ActiveXObject)
      {
      // code for IE6, IE5
      return new ActiveXObject("Microsoft.XMLHTTP");
      }
    return null;
    }
    Code (markup):
    
    <?php
    //get the q parameter from URL
    $q=$_GET["q"];
    
    	//Check if string is greater than 0, and return appropriate message
    	if (strlen($q) > 0)
     	 {
     	 if(!preg_match('/^[-a-z\x20]+$/i', stripslashes($q))) {
      		echo "<img src='../images/form_x.png' title='Error' alt='Error' /> .$q.";
    		}
    		else {
    		  echo "<img src='../images/form_checkmark.png' title='Checkmark' alt='Checkmark' /> .$q.";
    		}
    	}
    
    
    ?>
    Code (markup):
        First Name:
        <input type="text" id="F_Name" name="F_Name" size="32" value="<?php if(isset($_POST['F_Name'])){echo $_POST['F_Name'];}?>" onkeyup="checkAlpha(this.value , 'Fname')" /> <span id="alpha_check_fname"></span>
    Code (markup):
     
    tlshaheen, Feb 11, 2010 IP
  6. Brandon.Add.On

    Brandon.Add.On Peon

    Messages:
    178
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You need to escape the data before you send it through ajax. You could write an emulated PHP URL Encoded function in javascript. And in the PHP script decode it.
     
    Brandon.Add.On, Feb 11, 2010 IP
  7. tlshaheen

    tlshaheen Peon

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thank you! I'm new to Ajax, so still learning - after some searching, I've added the following line to the beginning of my checkAlpha function in my JS. I read that when using this encode, I don't need to decode it in my PHP. It now detects #, $, and other signs and properly handles them.

    str = encodeURIComponent(str);
    
    Code (markup):
    Now my only problem is it still gives me a error when I type "%". Do I need to use a different encoder to account for the "%" symbol? For reference, it gives me the following error:
     
    tlshaheen, Feb 11, 2010 IP
  8. tlshaheen

    tlshaheen Peon

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    /bump
    Anyone know why encodeURIComponent isn't encoding the symbol %?
     
    tlshaheen, Feb 12, 2010 IP
  9. JoeWJ

    JoeWJ Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I have alot of preg match trouble to. Looks like correct commands but it never seems to work.

    Could anyone go into a bit more detail about it?
     
    JoeWJ, Feb 12, 2010 IP
  10. tlshaheen

    tlshaheen Peon

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    /bump
    Anyone know why encodeURIComponent isn't encoding the symbol %?
     
    tlshaheen, Feb 15, 2010 IP
  11. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #11
    If all you want to do is check if it only contains letters and spaces, use:
    
    <?php
    if(!preg_match('/^([-a-z ]*)$/i', stripslashes($q))) {
    //echo error msg
    } else {
    //valid, theirfore proceed...
    }
    ?>
    
    PHP:
    I don't see why you need the \x20 ...
     
    danx10, Feb 15, 2010 IP
  12. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #12
    Woops, crossed this topic today, I forgot to remove the '-' from the regex - don't know what i was thinking.

    <?php
    if(!preg_match('/^([a-z ]*)$/i', stripslashes($q))) {
    //echo error msg
    } else {
    //valid, theirfore proceed...
    }
    ?>
    PHP:
     
    danx10, Feb 16, 2010 IP