<?php
function get_valid_url( $url ) {
$regex = “((https?|ftp)\:\/\/)?”; // Scheme
$regex .= “([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?”; // User and Pass
$regex .= “([a-z0-9-.]*)\.([a-z]{2,3})”; // Host or IP
$regex .= “(\:[0-9]{2,5})?”; // Port
$regex .= “(\/([a-z0-9+\$_-]\.?)+)*\/?”; // Path
$regex .= “(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?”; // GET Query
$regex .= “(#[a-z_.-][a-z0-9+\$_.-]*)?”; // Anchor
return preg_match(“/^$regex$/”, $url);
}
/*
Name Field VALIDATIONS
*/
function cf7_custom_form_validation($result,$tag) {
$type = $tag[‘type’];
$name = $tag[‘name’];
if($type == ‘text*’ && $_POST[$name] == ”){
$result[‘valid’] = false;
$result[‘reason’][$name] = wpcf7_get_message( ‘invalid_required’ );
}
$allNames = array(‘fullName’, ‘fullName1’);
foreach($allNames as $uniNames) {
if($name == $uniNames) {
$fullName = $_POST[$uniNames];
if($fullName != ” || $fullName == ”) {
if(!preg_match(‘/^[A-Z0-9][a-zA-Z0-9 ]+$/i’, $fullName)) {
$result[‘valid’] = false;
$result[‘reason’][$name] = ‘Please Enter a Valid Name’;
}
if(is_numeric($fullName)){
$result[‘valid’] = false;
$result[‘reason’][$name] = ‘Please Enter a Valid Name’;
}
}
}
}
return $result;
}
add_filter(‘wpcf7_validate_text’,’cf7_custom_form_validation’, 10, 2); // text field
add_filter(‘wpcf7_validate_text*’, ‘cf7_custom_form_validation’, 10, 2); // Req. text field
/*
TEXTAREA VALIDATIONS
*/
function cf7_custom_textarea_validation($result, $tag) {
$type = $tag[‘type’];
$name = $tag[‘name’];
if($type == ‘textarea*’ && $_POST[$name] == ”){
$result[‘valid’] = false;
$result[‘reason’][$name] = wpcf7_get_message( ‘invalid_required’ );
}
if($name == ‘addressTa’){
$addressTa = $_POST[‘addressTa’];
if($addressTa == ”) {
if(!preg_match(‘/^[A-Z0-9][a-zA-Z0-9 ]+$/i’, $fullName)) {
$result[‘valid’] = false;
$result[‘reason’][$name] = ‘Please Enter Your Comment’;
}
}
if($addressTa != ” ) {
if(strlen($addressTa) > 300) {
$result[‘valid’] = false;
$result[‘reason’][$name] = ‘You should not exceed 300 characters length’;
}
}
}
return $result;
}
add_filter( ‘wpcf7_validate_textarea’, ‘cf7_custom_textarea_validation’, 10, 2 );
add_filter( ‘wpcf7_validate_textarea*’, ‘cf7_custom_textarea_validation’, 10, 2 );
function cf7_custom_email_validation($result, $tag) {
$type = $tag[‘type’];
$name = $tag[‘name’];
if($type == ’email*’ && $_POST[$name] == ”){
$result[‘valid’] = false;
$result[‘reason’][$name] = wpcf7_get_message( ‘invalid_required’ );
}
if($name == ’emailAddress’) {
$emailAddress = $_POST[’emailAddress’];
if($emailAddress == ”) {
if(!preg_match(‘/^[A-Z0-9][a-zA-Z0-9 ]+$/i’, $fullName)) {
$result[‘valid’] = false;
$result[‘reason’][$name] = ‘Please Enter A Valid Email’;
}
}
if($emailAddress != ”) {
if(substr($emailAddress, 0, 1) == ‘.’ || !preg_match(‘/^([*+!.&#$¦\’\\%\/0-9a-z^_`{}=?~:-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,4})$/i’, $emailAddress)) {
$result[‘valid’] = false;
$result[‘reason’][$name] = ‘Entered Email is Invalid.’;
}
}
}
return $result;
}
add_filter( ‘wpcf7_validate_email’, ‘cf7_custom_email_validation’, 10, 2 );
add_filter( ‘wpcf7_validate_email*’, ‘cf7_custom_email_validation’, 10, 2 );
?>