Jump to content

Webform validation functions


mraquinn

Recommended Posts

Hi,

 

I'm working on a Drupal-based site and creating a contact form with Webform.  I have these two snippets of code to add to the form to validate the email addresses; the first checks the email address is a valid one (to a small extent) and the second one compares the two given addresses to ensure they are the same.

 

What I can't do is combine these into one function so that I can use it in Webform.  Could anyone help please?

 

Also, any suggestions on making the first function better are appreciated.

 

Thanks!

 

<?php

$email_address = $form_values['submitted_tree']['email_address'];

if (strlen(trim($email_address)) > 0) {

if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email_address))

form_set_error('submitted][email_address', t('Your email address is not valid.'));

};

?>

 

<?php
if ($form_values['submitted_tree']['email1'] != $form_values['submitted_tree']['email2']) {
form_set_error('submitted][email1', t('E-mail addresses must match.'));
}
?>

Link to comment
https://forums.phpfreaks.com/topic/139826-webform-validation-functions/
Share on other sites

You mean something like this?

 

<?php
function checkEmail($email1,$email2){
$email_address = $email1;
$email_address_check - $email2;
if (strlen(trim($email_address)) > 0) {
	if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email_address))
		return form_set_error('submitted][email_address', t('Your email address is not valid.'));
}
if ($email_address !== $email_address_check) {
	return form_set_error('submitted][email1', t('E-mail addresses must match.'));
}
return TRUE;
}

$result = checkEmail($form_values['submitted_tree']['email1'], $form_values['submitted_tree']['email2']);
?>

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.