Adding custom validation to form fields


#62

This post was flagged by the community and is temporarily hidden.


#63

Hi. I need to add custom validation to multiple form fields. Can I do this in one script? And should it be Before Body End Tag?

One of our developers sent us this code for the transitions but I’m not clear on how to incoporate it into this script. Thanks in advance for any help!

//Validating First Name

if( strlen(trim($_POST[‘first_name’])) > 0 ){
first_name = trim(_POST[‘first_name’]);
if ( $first_name != strip_tags($first_name) ){
array_push($error, ‘First Name do not allow HTML Tags’);
}
}else{
array_push($error, ‘First Name is required’);
}

//Validating Last Name

if( strlen(trim($_POST[‘last_name’])) > 0){
last_name = trim(_POST[‘last_name’]);
if ( $last_name != strip_tags($last_name) ){
array_push($error, ‘Last Name do not allow HTML Tags’);
}
}else{
array_push($error, ‘Last Name is required’);
}

//Validating Email Address

if( strlen(trim($_POST[‘email_address’])) > 0 ){
email = trim(_POST[‘email_address’]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
$lead = $enrollment->get_row(“SELECT et_lead_id FROM wp_et_leads where et_lead_email_address=’” . $enrollment->et_open_encrypt(strtolower($email)) . “’”);
//Checking any lead is already registered with this same email id.
if (isset($lead->et_lead_id) && $lead->et_lead_id != ‘’) {
array_push($error, ‘Email address already exist’);
$login = $enrollment->get_row(“SELECT et_short_code_slug FROM wp_et_short_codes where et_short_code_name = ‘[et_show_login_from_mail]’”);
//redirecting to the login page.
if (isset($login->et_short_code_slug) && $login->et_short_code_slug != ‘’) {
header(“Location: " . site_url($login->et_short_code_slug.’/?email=’.$email.’&action=emailexits’));exit;
}else{
header(“Location: " . site_url('login/?email=”’.$email.’”&action=emailexits’));exit;
}
}
}else{
array_push($error, ‘Invalid Email Address’);
}
}else{
array_push($error, ‘Email Address is required’);
}

//Validating Phone Number

if( strlen(trim($_POST[‘phone_number’])) > 0 ){
phone_number = trim(_POST[‘phone_number’]);
$numStripX = array(’(’, ‘)’, ‘-’, ‘.’, ‘+’,’ ‘);
$numCheck = str_replace($numStripX, ‘’, $phone_number);
$phone_number_int = $numCheck;
$firstNum = substr($numCheck, 0, 1);
$phone_valid = true;
if(($firstNum == 0) || ($firstNum == 1)) {$phone_valid = false;}
else if(!is_numeric($numCheck)){$phone_valid = false;}
else if(strlen($numCheck) > 10){$phone_valid = false;}
else if(strlen($numCheck) < 10){$phone_valid = false;}
else{
$formats = array(’###-###-####’, ‘### ### ####’, ‘(###) (###) (####)’, ‘(###) ###-####’, ‘(###)###-####’,’(###) ### - ####’, ‘##########’, ‘###.###.####’, ‘(###) ###.####’, ‘(###)###.####’);
$format = preg_replace("/[0-9]/", “#”, $phone_number);
if(in_array($format, $formats)){
$phone_valid = true;
}else{
$phone_valid = false;
}
}
if($phone_valid == false){
array_push($error, ‘Invalid Phone Number’);
}
}else{
array_push($error, ‘Phone Number is required’);
}

//Validating Zipcode

if(strlen(trim(_REQUEST['zip_code'])) > 0){ if(strlen(trim(_REQUEST[‘zip_code’])) >= 3 ){
zip = trim(_REQUEST[‘zip_code’]);
if(strlen($zip) < 5){
$zip = strlen($zip) == 3 ? (‘00’ . $zip) : (strlen($zip) == 4 ? (‘0’ . $zip) : $zip );
}
$states = $wpdb->get_row(“SELECT zip, state, city, service_status FROM wp_et_states WHERE zip = '” . $zip . “’ LIMIT 1”);
if(empty($states)){
array_push($error, ‘Invalid Zip Code’);
}else{
if($states->service_status < 1){
array_push($error, ‘We’re sorry, CESI does not offer debt management services in your state. Please visit our blog for free financial education and resources.’);
}else{
$state = $states->state;
$city = $states->city;
}
}
}else{
array_push($error, ‘Invalid Zip Code’);
}
}

//Validating State from zipcode

if(strlen(trim($state)) > 0){
if(trim($state) != ‘’) {
$states = $wpdb->get_row(“SELECT et_eligible_states_id,et_eligible_states_status,et_eligible_states_non_eligible_action FROM wp_et_eligible_states WHERE et_eligible_states_name = '” . $state . “’ LIMIT 1”);
if(isset($states) && $states->et_eligible_states_id != ‘’){
if($states->et_eligible_states_status==0 && $states->et_eligible_states_non_eligible_action==‘not-licensed’){
if(!in_array(‘We’re sorry, CESI does not offer debt management services in your state. Please visit our blog for free financial education and resources.’, $error)){
array_push($error, ‘We’re sorry, CESI does not offer debt management services in your state (’.$state.’). Please visit our blog for free financial education and resources.’);
}
}
}else{
if(!in_array(‘We’re sorry, CESI does not offer debt management services in your state. Please visit our blog for free financial education and resources.’, $error)){
array_push($error, ‘We’re sorry, CESI does not offer debt management services in your state (’.$state.’). Please visit our blog for free financial education and resources.’);
}
}
}
}


#64

Hi @KellyCESI,

The code your developer gave you looks like PHP.

You won’t be able to use it directly with your Unbounce landing page. You can only add/edit the front-end part of the page - HTML/CSS/JS.

You have a couple of options:

  1. Host this PHP script on your server and write a JS script that would ping it when a visitor tries to submit the form.

  2. Rewrite the above as a JS script

Best,
Hristian


#65

Thanks, Hristian! Go figure we are speaking 2 languages and I don’t speak either :wink:

I let our developer know.