Hi People,
I am looking for phone validation using the gravity forms plugin, minium 8 numbers for the field and no "text = abc" allowed, I know we can't validate every phone number :)
The ID of the phone field is "3"
Hope someone can help!
Jayaram Y answers:
can you try this in functions.php
add_filter( 'gform_field_validation_175_1', 'custom_validation', 10, 4 );
function custom_validation( $result, $value, $form, $field ) {
if ( $result['is_valid'] && intval( $value ) < 8 ) {
$result['is_valid'] = false;
$result['message'] = 'Please enter a value more than 8';
}
return $result;
}
Carl Brook comments:
That doesn't work Jayaram! BUT does it have something to do with NOT assigning the ID of the field??
Thanks
Arnav Joy answers:
check this
http://stackoverflow.com/questions/25649379/gravity-forms-validation-on-number-field
https://www.gravityhelp.com/documentation/article/gform_field_validation/
Carl Brook comments:
Hi Armav,
I tried this but this also didn't work
Carl Brook comments:
WHY are these NOT working! Ahhhhhhhhh
Kyle answers:
Try this
add_filter( 'gform_field_validation_##_8', '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 ) < 8 && is_numeric( $value ) ) {
$result['is_valid'] = false;
$result['message'] = 'You have entered an invalid phone number';
}
return $result;
}
Carl Brook comments:
Hi Kyle,
Thanks for jumping in, BUT this didn't work either!
Carl Brook comments:
Didnt work this KYLE.... Any ideas??
Kyle comments:
Carl,
Can you see my reply above and print out the $value field. Then please copy and paste the code as you have it in your functions.php file.
Carl Brook comments:
I put in exactly what you put above!
add_filter( 'gform_field_validation_##_8', '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 ) < 8 && is_numeric( $value ) ) {
$result['is_valid'] = false;
$result['message'] = 'You have entered an invalid phone number';
}
return $result;
}
Kyle comments:
Carl,
Did you see the note on the first line? You need to add your form ID in place of the ##
Also, please add
echo $value;
Bob answers:
gravity form already have phone field. why don't you use it?
There is also input mask functionality available.