How to make Gravity Forms name field input only accept letters - php

How do I make the gravity form name field only accept letters, numbers should give an error.

You need to use gform_field_validation validation filter to be able to do this kind of validation before the form submits.
In addition, we need to use preg_match function of PHP with a regex to ensure we are only taking Letters from Full Name input value.
Just add this code in your active theme functions.php file: (Code tested and working)
add_filter( 'gform_field_validation', function ( $result, $value, $form, $field ) {
if ( $field->type == 'name' ) {
// Input values
$fullName = rgar( $value, $field->id . '.3' );
if ( empty( $fullName )) {
$result['is_valid'] = false;
$result['message'] = empty( $field->errorMessage ) ? __( 'This field is required. Please enter a complete name.', 'gravityforms' ) : $field->errorMessage;
} else if (preg_match('/[A-Za-z].*[0-9]|[0-9].*[A-Za-z]/', $fullName)) { //check for letters only
$result['is_valid'] = false;
$result['message'] = empty( $field->errorMessage ) ? __( 'Full name must ony contains letters.', 'gravityforms' ) : $field->errorMessage;
} else {
$result['is_valid'] = true;
$result['message'] = '';
}
}
return $result; //return results
}, 10, 4 );

Related

Form validations for non-required fields

I'm trying to add some form validations to a site I'm working on. I was able to get the validations to work on the fields that are required but as I'm trying to adjust the code for the non-required fields that still need to be validated when there is user-input I'm running into some issues.
This works pretty flawlessly if the field is required but when I make the field not required, the form doesn't submit and puts out an error message if I leave the field blank.
add_action( 'elementor_pro/forms/validation', function ( $record, $ajax_handler ) {
$fields = $record->get_field( [
'id' => 'dot',
] );
if ( empty( $fields ) ) {
return;
}
$field = current( $fields );
if ( 1 !== preg_match( '/[1-9]{1}[0-9]{5,7}/', $field['value'] ) || $field['value'] == '123456' || $field['value'] == '12345678' ) {
$ajax_handler->add_error( $field['id'], 'Invalid DOT#' );
}
}, 10, 2 );

WooCommerce change order status using elementor forms

I am trying to build a form where when a user enters their order ID and unique serial code in the elementor form their order status is changed to completed. Heres the code i have written but i keep getting an error. The form has 2 fields 'a' & 'b'. A= order id and B= serial code.
add_action( 'elementor_pro/forms/validation', function ( $record, $ajax_handler ) {
$fields = $record->get_field( [
'id' => 'b'
], ['cid' => 'a'
]);
if ( empty( $fields ) ) {
return;
}
$field = current( $fields );
if ( 1 !== strlen( $field['value'] ) < 13 ) {
$ajax_handler->add_error( $field['id'], 'Invalid Serial Code, Please enter the 13 Digit Code mentioned on your delivery letter' );
} else {
$output['result'] = get_post_meta( $field['cid'], $key = 'shp_tkn', $single = true );
}
if ($output == $field['id']){
$order = $field['cid'] ;
$order->update_status('completed');
} else {
$ajax_handler->add_error( $field['id'], 'Invalid Serial Code, Please try again' );
}
}, 10, 2 );
If anyone could tell me where I'm going wrong.

Gravity form single line text field custom validation (numbers only with 10 or more digits)

Our web form built with Gravity Forms on WordPress uses a "single line text" field type for the phone number field.
How can I add a custom validation to this so that it will only allow submission of integers and only if there is 10 or more input?
This is because in Australia our phone numbers (without country code) are 10 digits. We're currently getting responses containing letters or incomplete numbers.
I've just implemented the below function but it doesn't work.
// Form Phone Field Validation to use numbers only with minimum 10 digits
add_filter( 'gform_field_validation_16_4', 'custom_validation', 10, 4 ); //Replace the ## with your form ID
function custom_validation( $result, $value, $form, $field ) {
$value = str_replace(array('(',')','-',' '),'',$value);//Remove hyphens, parenthesis, and spaces - you can take this out if you want strict numbers only
if ( $result['is_valid'] && strlen( $value ) < 10 && is_numeric( $value ) ) {
$result['is_valid'] = false;
$result['message'] = 'Please enter a valid phone number of 10 or more digits with area code';
}
return $result;
}
Based on documentation
you should be doing something like this,
//replace {FORM_ID} with your form ID
add_filter( 'gform_validation_{FORM_ID}', 'custom_validation' );
function custom_validation( $response ) {
$form = $response['form'];
//replace {INPUT_ID} with the input ID you want to validate
$value = str_replace(array('(',')','-',' '), '', rgpost( 'input_{INPUT_ID}' ) );
if ( strlen( $value ) < 10 || !is_numeric( $value ) ) {
$response['is_valid'] = false;
//finding Field with ID and marking it as failed validation
foreach( $form['fields'] as &$field ) {
//NOTE: replace {INPUT_ID} with the field you would like to validate
if ( $field->id == '{INPUT_ID}' ) {
$field->failed_validation = true;
$field->validation_message = 'Please enter a valid phone number of 10 or more digits with area code';
break;
}
}
}
//Assign modified $form object back to the validation result
$response['form'] = $form;
return $response;
}
Try to use function "gform_validation" to add the filter:
add_filter( 'gform_validation', 'custom_validation' );
function custom_validation( $validation_result ) {
//your code here
}
Also see docs here

Issues in validating custom code in Contact Form 7

I'm trying to implement a text field validation for field name VoucherNumber that requires the code to be in a certain pattern which is 'WWV-'followed by 4 numbers.
I was successfully able to implement this on google docs using the following expression ^[W]WV-[0-9][0-9][0-9][0-9].
I researched through various answers and attempted to add this code in functions.php but it didn't work. It would just show that the form is being sent (spinning wheel) but it would not be sent even after 5 minutes.
add_filter( 'wpcf7_validate_text*', 'validate_voucher_number', 20, 2 );
function validate_voucher_number( $result, $tag ) {
$tag = new WPCF7_FormTag( $tag );
if ( 'VoucherNumber' == $tag->name ) {
$VoucherNumber = isset( $_POST['VoucherNumber'] ) ? trim( $_POST['VoucherNumber'] ) : '';
if ( ! preg_match ( "^[W]WV-[0-9][0-9][0-9][0-9]" , $VoucherNumber) ){
$result->invalidate( $tag, "Voucher number is invalid" );
}
}
return $result;
}
Just make sure that your field name is VoucherNumber and try this code :
add_filter( 'wpcf7_validate_text', 'vouchervalidation', 20, 2 );
function vouchervalidation( $result, $tag ) {
if ( 'VoucherNumber' == $tag->name ) {
$VoucherNumber = isset( $_POST['VoucherNumber'] ) ? trim( $_POST['VoucherNumber'] ) : '';
if ( ! preg_match ( "^[W]WV-[0-9][0-9][0-9][0-9]" , $VoucherNumber) ){
$result->invalidate( $tag, "Voucher number is invalid" );
}
}
return $result;
}

Validate at least one field to be filled in, Gravity forms

I have been trying to create a custom validation hook for Gravity Forms plugin.
The validation checks that at least one field has been filled in from a set of fields.
Check out the code below, I just can't get it too work. I think it is something to do with the variables for the inputs, even if a field is filled in, the error still shows on each field?
add_filter( 'gform_field_validation_2', function ( $result, $value, $form, $field ) {
if ( $field->type == 'number') {
$a = rgar( $value, $field->id . '10' );
$b = rgar( $value, $field->id . '12' );
$c = rgar( $value, $field->id . '13' );
$d = rgar( $value, $field->id . '14' );
$e = rgar( $value, $field->id . '15' );
$f = rgar( $value, $field->id . '17' );
$g = rgar( $value, $field->id . '18' );
$h = rgar( $value, $field->id . '20' );
$i = rgar( $value, $field->id . '21' );
$j = rgar( $value, $field->id . '22' );
$k = rgar( $value, $field->id . '23' );
if ( !empty($a) || !empty($b) || !empty($c) || !empty($d) || !empty($e) || !empty($f) || !empty($g) || !empty($h) || !empty($i) || !empty($j) || !empty($k) ) {
$result['is_valid'] = true;
$result['message'] ='';
} else {
$result['is_valid'] = false;
$result['message'] = 'Please select a quantity of materials to order';
}
}
return $result;
}, 10, 4 );
I think you maybe should use a field of type "radio buttons".
Anyway, if your form has several "number" fields and you need to validate that at least one of them has been filled, then you should use the gform_validation filter since you're validating the whole form, not just a single field.
TIP: Add a custom css class to each field in the group to identify them. for example "validate-quantity".
add_filter('gform_validation_2', 'quantity_validation', 1, 4);
function quantity_validation($validation_result) {
if ($validation_result['is_valid']) {
$valid=false;
$form = $validation_result['form'];
foreach( $form['fields'] as &$field ) {
if ( strpos( $field->cssClass, 'validate-quantity' ) === false ) {
continue;
}
$field_value = rgpost( "input_{$field['id']}" );
if (!empty($field_value)) {
$valid=true;
break;
}
}
if (!$valid) {
$field["failed_validation"] = true;
$field["validation_message"] = 'Please select a quantity of materials to order';
$validation_result['form'] = $form;
}
}
return $validation_result;
}
So heres a working version (Thanks for the gform_validation hint Francisco R) - Went down a slightly different route, but works perfectly, incase anyone interested in future!
add_filter( 'gform_validation_2', 'custom_validation_2' );
function custom_validation_2( $validation_result ) {
// array of field IDs to be checked
$field_ids = array (10, 12, 13, 14, 15, 17, 18, 20, 21, 23, 22);
// get the form object from the validation result
$form = $validation_result['form'];
// counter to store how many fields have a value > 0 submitted
$number_of_fields = 0;
// loop through all the fields to be sure one has a value > 0
foreach ( $field_ids as $input ) {
// the rgpost string we are going to check
$input_id = 'input_' . intval( $input );
// the value that was submitted
$input_value = rgpost ( $input_id );
if ( $input_value > 0 ) {
// if any field in the array has a value, we can just continue
$number_of_fields++;
} // end if
else {
// no value for this input, so continue without incrementing the counter
continue;
} // end else
} // end foreach
// check the $number_of_fields and if it is 0 return a validation error
if ( $number_of_fields == 0 ){
// set the form validation to false
$validation_result['is_valid'] = false;
// mark all the fields with a validation error
foreach( $form['fields'] as &$field ) {
// add a validation error to *all* the inputs if none were submitted > 0
if ( in_array( $field->id, $field_ids ) ) {
$field->failed_validation = true;
$field->validation_message = 'Please select a quantity of materials to order from one or all of these fields.';
} // end if
} // end foreach
} // end if
// assign modified $form object back to the validation result
$validation_result['form'] = $form;
return $validation_result;
}
It looks you should change your if clause to:
if ( empty($a) || empty($b) || empty($c) || empty($d) || empty($e) || empty($f) || empty($g) || empty($h) || empty($i) || empty($j) || empty($k) ) {
to validate if at least one option is selected.
To skip not targeted fields add the following code before complex if above:
$target_fields = array('name_1', 'name_2');
if (!in_array($field, $target_fields)) {
$result['is_valid'] = true;
$result['message'] = '';
}

Categories