WordPress.org

Ready to get started?Download WordPress

Ideas

Contact form Name field should be accept alphabetical values

  1. jayaprakash.jp
    Member

    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' );
    }

    //__________________________________________________________________________________________________
    //Comparision date

    $date1 = $_POST['date1'];
    $date2 = $_POST['date2'];

    //CheckInDate
    if($name == 'date1'){
    if(!compare_date($date1, $date2)) {
    $result['valid'] = false;
    $result['reason'][$name] = 'Logical Error: Check in date should be before check out date';
    }
    }

    //__________________________________________________________________________________________________

    //CheckOutDate
    if($name == 'date2'){
    if(!compare_date($date1, $date2)) {
    $result['valid'] = false;
    $result['reason'][$name] = 'Logical Error: Check out date should be After check in date';
    }
    }

    //__________________________________________________________________________________________________

    //url
    if($name == 'url') {
    $url = $_POST['url'];

    if($url != '') {
    if(get_valid_url($url)){
    $result['valid'] = true;
    } else {
    $result['valid'] = false;
    $result['reason'][$name] = 'Entered URL is invalid.';
    }
    }
    }

    //__________________________________________________________________________________________________

    //emailAddress
    if($name == 'emailAddress') {
    $emailAddress = $_POST['emailAddress'];

    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.';
    }
    }
    }

    //__________________________________________________________________________________________________

    //datemdy
    if($name == 'datemdy'){
    $datemdy = $_POST['datemdy'];

    if($datemdy != '') {
    if(!preg_match('/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/', $datemdy)) {
    $result['valid'] = false;
    $result['reason'][$name] = 'Enter Date in MM/DD/YYYY Format';
    }
    }
    }
    //__________________________________________________________________________________________________
    //US Zip code validation
    //USZipCode
    if($name == 'USZipCode') {
    $USZipCode = $_POST['USZipCode'];

    if($USZipCode != '') {
    // if(!preg_match('/^([0-9]{5})(-[0-9]{4})?$/i', $USZipCode)) {
    if(!preg_match('/^\d{5}(-\d{4})?$/', $USZipCode)) {
    $result['valid'] = false;
    $result['reason'][$name] = 'Entered Zipcode is Invalid';
    }
    }
    }

    //__________________________________________________________________________________________________
    // CANADA Zip code validation
    //CANZipCode
    if($name == 'CANZipCode') {
    $CANZipCode = $_POST['CANZipCode'];

    if($CANZipCode != '') {
    if(!preg_match('/^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$/', $CANZipCode)) {
    $result['valid'] = false;
    $result['reason'][$name] = 'Entered Zipcode is Invalid';
    }
    }
    }

    //__________________________________________________________________________________________________
    // US and CANADA Zip code validation
    //USCANZipCode
    if($name == 'USCANZipCode') {
    $USCANZipCode = $_POST['USCANZipCode'];

    if($USCANZipCode != '') {
    if(!preg_match('/^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$/', $USCANZipCode)) {
    if(!preg_match('/^\d{5}(-\d{4})?$/', $USCANZipCode)) {
    $result['valid'] = false;
    $result['reason'][$name] = 'Entered Zipcode is Invalid';
    }
    }
    }
    }

    //__________________________________________________________________________________________________
    // Indian Postal code validation
    //inPostalCode
    if($name == 'inPostalCode') {
    $inPostalCode = $_POST['inPostalCode'];

    if($inPostalCode != '') {
    if(!preg_match('/^[0-9]{6,6}$/', $inPostalCode)) {
    $result['valid'] = false;
    $result['reason'][$name] = 'Entered Pin code for India is Invalid';
    }
    }
    }

    //__________________________________________________________________________________________________

    //This section updated on 22nd March 2013
    // It will accept character, character + numeric value
    // It will not accept special characters

    //fullName
    $allNames = array('fullName', 'fullName1');
    foreach($allNames as $uniNames) {
    if($name == $uniNames) {
    $fullName = $_POST[$uniNames];

    if($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';
    }
    }

    }
    }
    //__________________________________________________________________________________________________

    //acceptNum
    $acceptNumbers = array('acceptNumber', 'acceptNumber1', 'acceptNumber2', 'acceptNumber3', 'acceptNumber4', 'acceptNumber5', 'acceptNumber6');

    foreach($acceptNumbers as $acceptNumber){
    if($name == $acceptNumber) {
    $acceptNum = $_POST[$acceptNumber];

    if($acceptNum != '') {
    if(ctype_digit($acceptNum)) {
    $result['valid'] = true;
    } else {
    $result['valid'] = false;
    $result['reason'][$name] = 'Please Enter Only Numbers';
    }
    }
    }
    }

    //__________________________________________________________________________________________________

    //faxNumber
    $faxNumbers = array('faxNumber', 'faxNumber1', 'faxNumber2', 'faxNumber3', 'faxNumber4', 'faxNumber5', 'faxNumber6');
    foreach($faxNumbers as $faxNum) {
    if($name == $faxNum) {
    $faxNumber = $_POST[$faxNum];
    $contRegex = '/^(?:1(?:[. -])?)?(?:\((?=\d{3}\)))?([2-9]\d{2})(?:(?<=\(\d{3})\))? ?(?:(?<=\d{3})[.-])?([2-9]\d{2})[. -]?(\d{4})(?: (?i:ext)\.? ?(\d{1,5}))?$/';
    if ($faxNumber != '') {
    if(!preg_match($contRegex, $faxNumber) && strlen($faxNumber) > 10 && strlen($faxNumber) < 18) {
    $result['valid'] = false;
    $result['reason'][$name] = 'Entered Fax Number is Invalid';
    }
    }
    }
    }

    //__________________________________________________________________________________________________

    //Agreement fields with the expected values. Ex. write ,,yes" to agree
    $agreement = array('agreement','agreement1','agreement2');
    $expectedValues = array('yes', 'Yes', 'YES');
    foreach ($agreement as $a => $aValue) {
    if($name == $aValue) {
    foreach ($expected_values as $i => $eValue) {
    if($eValue == $_POST[$name]) {
    $result['valid'] = true; break;
    } else {
    $result['valid'] = false;
    $result['reason'][$name] = 'You did not agree with the terms';
    }
    }
    }
    }

    //__________________________________________________________________________________________________

    //Only Characters
    $OnlyChars = array('onlyChar', 'onlyChar1', 'onlyChar2');
    foreach($OnlyChars as $OnlyChar){
    if($name == $OnlyChar) {
    $onlyChar = $_POST[$OnlyChar];

    if($onlyChar != '') {
    $containsLettersOrNumbers = (preg_match('~[0-9a-z]~i', $onlyChar) > 0);
    if(!$containsLettersOrNumbers
    || is_numeric($onlyChar)
    || strlen($onlyChar > 64) ) {
    $result['valid'] = false;
    $result['reason'][$name] = 'Please Enter Only Characters';
    }

    if(is_numeric($onlyChar)){
    $result['valid'] = false;
    $result['reason'][$name] = 'Please Enter Only Characters';
    }
    }

    }
    }

    //__________________________________________________________________________________________________

    //validPhone
    /*
    49-4312 / 777 777
    +1 (305) 613-0958 x101
    (305) 613 09 58 ext 101
    3056130958
    +33 1 47 37 62 24 extension 3
    (016977) 1234
    04312 - 777 777
    91-12345-12345
    +58 295416 7216
    */

    $phoneNumbersAll = array('validPhone', 'validPhone1', 'validPhone2', 'validPhone3', 'validPhone4', 'validPhone5', 'validPhone6');

    foreach($phoneNumbersAll as $validPhoneVal) {
    if($name == $validPhoneVal) {
    $validPhone = $_POST[$validPhoneVal];

    if($validPhone != '') {
    if(preg_match('/^(?:1(?:[. -])?)?(?:\((?=\d{3}\)))?([2-9]\d{2})(?:(?<=\(\d{3})\))? ?(?:(?<=\d{3})[.-])?([2-9]\d{2})[. -]?(\d{4})(?: (?i:ext)\.? ?(\d{1,5}))?$/', $validPhone)

    || preg_match('/^([\+][0-9]{1,3}[\ \.\-])?([\(]{1}[0-9]{2,6}[\)])?([0-9\ \.\-\/]{3,20})((x|ext|extension)[\ ]?[0-9]{1,4})?$/', $validPhone)

    && strlen($validPhone) > 9

    && strlen($validPhone) < 30

    && (int)($validPhone)) {
    //$result['valid'] = true;
    } else {
    $result['valid'] = false;
    $result['reason'][$name] = 'Entered Phone Number is Invalid';
    }
    }
    }
    }

    //__________________________________________________________________________________________________
    return $result;
    }

    //add filter for text field validation
    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

    ===========================
    <p>Name
    [text* onlyChar] </p>

    Posted: 10 months ago #
  2. Ipstenu (Mika Epstein)
    Half-Elf Support Rogue & Mod

    This is not a CORE WP suggestion, but for a plugin. Also please don't post long blocks of code here. Use gist or pastebin.

    Posted: 10 months ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.

  • Rating

    12345
    0 Votes
  • Status

    This is plugin territory