Adding custom validation to form fields


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


#66

Hi @Johnny_Opao,

Do you have anything similar for Radio Buttons / Checkboxes? I.e. Can you set the “error message” to be triggered if Option A is selected instead of Option B on a certain checkbox in the form? Thanks, Nico


#67

@Mark_Wainwright Thanks for a great solution, but I found an issue and dont know how to fix it.

Trying to implement this on multistep form (Multi-Field, Multi-Step Form 🔥). But validation pops up right in the beginning (even though the validation field is at the end of the multistep form), without letting me to proceed. could you help me out please :slight_smile:

Thanks!


#68

Hi @Hristian,

I’m trying to use the above script to validate two radio buttons on my landing pages form. The radio button options are “YES” and “NO”. I need the lead to click on the “YES” option in order to proceed and successfully click the CTA button, registering with us. However, I’ve tried dozens of variations of the script found on the unbounce community pages but none of them seem to work! I really need some help with this one!!


#69

Hi @nico,

You’ll need to modify both of your scripts - the multi-step and the custom validation to work together.

Best,
Hristian


#70

Hi @Hristian

Thanks for getting back to me. Unfortunately I’m a complete noob at coding with Jquery. Is there anyway I could hire a freelancer to do this for me through the unbounce community?

Cheers,
Nico


#71

Hello,

I was wondering if Unbounce’s decision to remove jQuery from the landing pages would affect the validation scripts shared in this thread.

If Unbounce is removing jQuery from the lp object then what would be their alternative rather than using jQuery’s .validator plugin which is what is currently recommended?

Any further direction would be appreciated.

Thank you

  • Jon S.

#72

Could you give an example of this for a custom message that displays whenever someone selects a “Yes” radio button in a group and shows no message when someone selects the “No” radio button in the same group.

I’m trying to make give a custom message when someone selects “Yes” for each of 2 separate radio button questions.