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):
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.
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.
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):
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.
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:
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?
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 ...
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: