Jump to content

contact form - easy question for most of you


greentrancer

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

^ 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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.