greentrancer Posted February 10, 2012 Share Posted February 10, 2012 hello. i want to know what is *$ or +$ because i see it in many contact forms. also, what is the difference between these: /^[A-Z]+[a-z]*$/ /^[A-Z]+[a-z]+$/ /^[A-Z]+[a-z]$/ /^[A-Z]+[a-z]/ also, which is the shortest and efficient method of checking somebodys name to be exactly "Name Surname" - "Green Trancer" ? in my form, i use var filter = /^[A-Z]+[a-z]*$/; var filter2 = /^[A-Z]+[a-z]+\s+[A-Z]+[a-z]+$/; and then i check the name to be like filter or filter2. how can i check that between the words to be exactly one space character ? thank you very much. Quote Link to comment https://forums.phpfreaks.com/topic/256810-contact-form-easy-question-for-most-of-you/ Share on other sites More sharing options...
AyKay47 Posted February 10, 2012 Share Posted February 10, 2012 in these regex examples, the + and * are repetition operators, the + after the character class [a-z] tells the regex engine to search for the character class at least one time (greedy). The * after the character class [a-z] tells the engine to search for the class 0 or more times (greedy), in essence making the character class optional (however, since the * is greedy, it will attempt to find as many character matches as possible). The $ is an end of string anchor, a zero-width positional character that will match the void space after the last character in a string. Quote Link to comment https://forums.phpfreaks.com/topic/256810-contact-form-easy-question-for-most-of-you/#findComment-1316533 Share on other sites More sharing options...
Berre Posted February 10, 2012 Share Posted February 10, 2012 ^ means that it must be the beginning of the string, and the $ is the opposite, meaning the end of the string. /^[A-Z]+[a-z]*$/ This one will match strings that starts with capital letters. The + sign means that there must be at least one capital letter. After that it CAN match lowercase letters. The * means zero or more hits. This regex would match for example: A FE FFDXFGRGRG Grsgscgf GFEa Gg To match a string containing two names (first and last name), and want to ignore capitalization you could use /^[a-z]+ [a-z]+$/i (the i at the end means case-insensitive). If you use \s it will match other white-space as well (not only space), like tabulator. Quote Link to comment https://forums.phpfreaks.com/topic/256810-contact-form-easy-question-for-most-of-you/#findComment-1316538 Share on other sites More sharing options...
AyKay47 Posted February 10, 2012 Share Posted February 10, 2012 to answer the edit to your thread that you have made, only one regex is needed for this, not sure why you have two. Your regex is a little sloppy, and will allow a name like "Green Trancer" so I will clean it up. The multiple character classes are not needed, as you can simply append the "case insensitive" modifier to the pattern. Also, you should have a specific range on the character class, there must be a min and max amount of characters allowed, correct? $str = "Green Trancer"; $pattern = "~^[a-z]{2,12}\s[a-z]{2,20}$~i"; preg_match($pattern,$str,$ms); print_r($ms); This regex has the following rules: 1. the first name must be [a-zA-Z] and between 2 and 12 characters (taking into account weird 2 letter first names) 2. there must be a space, and only one space following the first name. 3. the last name can only be [a-zA-Z] and between 2 and 20 characters. (again, taking into consideration weird last names.) 4. The full name must be the only part of the subject, if there is any other text in the string, the regex will fail as expected. Quote Link to comment https://forums.phpfreaks.com/topic/256810-contact-form-easy-question-for-most-of-you/#findComment-1316540 Share on other sites More sharing options...
greentrancer Posted February 10, 2012 Author Share Posted February 10, 2012 thank you for the answers, a little too complicated for me, but i understood the most of it. i use 2 regex because the user can enter only one name like "Green" or 2 names like "Green Trancer". now i understand better what is happening in that regex and i remade it for my form: var b = $("#name").val(); var filter = /^[A-Z]+[a-z]+$/; var filter2 = /^[A-Z]+[a-z]+\s[A-Z]+[a-z]+$/; //if it's valid email if((filter.test(b) || filter2.test(b)) && b.length > 2) thank you very much for the answers. if you want to add something, feel free to do it. thanks, again! Quote Link to comment https://forums.phpfreaks.com/topic/256810-contact-form-easy-question-for-most-of-you/#findComment-1316551 Share on other sites More sharing options...
AyKay47 Posted February 10, 2012 Share Posted February 10, 2012 thank you for the answers, a little too complicated for me, but i understood the most of it. i use 2 regex because the user can enter only one name like "Green" or 2 names like "Green Trancer". now i understand better what is happening in that regex and i remade it for my form: var b = $("#name").val(); var filter = /^[A-Z]+[a-z]+$/; var filter2 = /^[A-Z]+[a-z]+\s[A-Z]+[a-z]+$/; //if it's valid email if((filter.test(b) || filter2.test(b)) && b.length > 2) thank you very much for the answers. if you want to add something, feel free to do it. thanks, again! yeah, you still will only need one regex for this: $pattern = "~^[a-z]{2,12}(\s[a-z]{2,20})?$~i"; this pattern will attempt to match a first and last name first, if it doesn't find one, it will match the first name (with trailing spaces disallowed) Quote Link to comment https://forums.phpfreaks.com/topic/256810-contact-form-easy-question-for-most-of-you/#findComment-1316555 Share on other sites More sharing options...
greentrancer Posted February 12, 2012 Author Share Posted February 12, 2012 thank you for the hint with "?" - i converted it for my needs: var filter = /^([A-Z]{1,1})([a-z]{2,20})(\s[A-Z]{1,1}[a-z]{2,20})?$/; thank you again! Quote Link to comment https://forums.phpfreaks.com/topic/256810-contact-form-easy-question-for-most-of-you/#findComment-1317356 Share on other sites More sharing options...
greentrancer Posted February 12, 2012 Author Share Posted February 12, 2012 ok, now i have another question about the mail validation. mine looks like this: var filter = /^[a-z0-9]+[a-z0-9_.-]*[a-z0-9]+@[a-z0-9]+[a-z0-9.-]*[a-z0-9]+.[a-z]{2,6}$/; - the question is: what do you think ? add something ? do you have a better one ? thanks! Quote Link to comment https://forums.phpfreaks.com/topic/256810-contact-form-easy-question-for-most-of-you/#findComment-1317378 Share on other sites More sharing options...
AyKay47 Posted February 13, 2012 Share Posted February 13, 2012 ok, now i have another question about the mail validation. mine looks like this: var filter = /^[a-z0-9]+[a-z0-9_.-]*[a-z0-9]+@[a-z0-9]+[a-z0-9.-]*[a-z0-9]+.[a-z]{2,6}$/; - the question is: what do you think ? add something ? do you have a better one ? thanks! actually, PHP has numerous built-in regex's that are available for validation and filtering. Take a look at filter_var, the example includes the email validation syntax. Quote Link to comment https://forums.phpfreaks.com/topic/256810-contact-form-easy-question-for-most-of-you/#findComment-1317396 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.