Jump to content

Expected string for form validation


lewis1682

Recommended Posts

Hey, I have created a contact us form which works nicely with my site, however I have some backup PHP validation code, which all works apart from the last name section, how can I set it up to it checks to see if there is a first name and a last, maybe even another name? This is what I have so far and it only works if you put in a first name and nothing else:

	$string_exp = "/^[A-Za-z.'-]+\$/";
	if(!preg_match($string_exp, $name)) {
		$error_message .= 'The Name you entered does not appear to be valid.<br/>';
	}

Thank you

Link to comment
https://forums.phpfreaks.com/topic/284261-expected-string-for-form-validation/
Share on other sites

Kind of hard for us to provide any real help without knowing where the first name and last name are defined. All I see is a variable called $name. Are you concatenating the first and last name fields before doing the validation?

 

Also, I would ask why you care what data is in the name fields? Do you realize that you would potentially restrict valid names? For example, what about letters with accents such as "Muñoz". It gets even more complicated when dealing with character sets that are not Latin based. Now, there could be valid reasons to limit the valid characters in the name fields. But, if you don't have a valid business reason for doing so, it is not worth the effort - in my opinion.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.