Đoạn code bên dưới sẽ giúp bạn ràng buộc số điện thoại Việt Nam trong contact form 7. Mục đích là để tránh các công cụ spam form liên hệ. Các bạn hãy làm theo các bước như mục lục ở trên nha!

B1: Tạo Form liên hệ trong contact form 7

Bạn hãy dán đoạn code liên hệ mẫu vào Contact Form 7 theo hình hướng dẫn bên dưới:

<div class="form-row">
	<div class="form-group col-md-6">
		[text* your-name class:form-control placeholder "Họ và tên"]
	</div>
	<div class="form-group col-md-6">
		[tel* your-phone class:form-control placeholder "Số điện thoại"]
	</div>
</div>
<div class="form-row">
	<div class="form-group col-md-6">
		[email* your-mail class:form-control placeholder "Email"]
	</div>
	<div class="form-group col-md-6">
		[text* your-subject class:form-control placeholder "Tiêu đề"]
	</div>
</div>
<div class="form-row">
	<div class="form-group col-md-12">
		[textarea your-message class:form-control placeholder "Yêu cầu"]
	</div>
</div>
<div class="text-center">[submit "Gửi liên hệ"]</div>

B2: Thêm ràng buộc số điện thoại Việt Nam

Bạn hãy dán đoạn code sau vào file functions.php

function tmdev_validate_phone_vietnam( $result, $tag ) { 
	$name = $tag->name; 
	$value = isset( $_POST[$name] ) ? trim( wp_unslash( strtr( (string) $_POST[$name], "\n", " " ) ) ) : ''; 
	$errorMessage = 'Số điện thoại không hợp lệ!'; 
	if ( 'tel' == $tag->basetype ) { 
		if( !preg_match('/^(03|05|07|08|09)+[0-9]{8}$/', $value ) ){ 
			$result->invalidate( $tag, $errorMessage ); 
		} 
	} 
	return $result; 
} 
add_filter('wpcf7_validate_tel*', 'tmdev_validate_phone_vietnam', 10, 2);

Hoặc bạn cũng có thể ràng buộc một số thông tin khác theo cách này vd:

add_filter('wpcf7_validate_text*', 'tmdev_validate_text_your_name', 10, 2);
function tmdev_validate_text_your_name(  $result, $tag ){
    $name = $tag->name; 
    $value = isset( $_POST[$name] ) ? trim( wp_unslash( strtr( (string) $_POST[$name], "\n", " " ) ) ) : '';

    // rang buoc ho ten va ten doanh nghiep khong co ca ky tu dac biet
    if( $tag->name === 'your-name' || $tag->name === 'your-company' ){
        $errorMessage = "Vui lòng không thêm các ký tự đặc biệt";  
        if ( preg_match('/[()*~@!#$%^&_\-\+=\?\{\}\[\]\`\<\>\d]/', $value ) ){
           $result->invalidate( $tag, $errorMessage ); 
        }  
    }  
    // rang buoc ma so thue
    if( $tag->name === 'your-tax-number' ){
        $errorMessage = "Mã số thuế không hợp lệ";  
        if( !preg_match('/^[0-9]{10}$/', $value ) ){ 
            $result->invalidate( $tag, $errorMessage ); 
        }  
    }       
  
    return $result;
}

B3: Kiểm tra kết quả Ràng buộc số điện thoại Việt Nam

Sau khi thêm xong B2 ở trên bạn hãy dán đoạn shortcode vào trang cần hiển thị form liên hệ theo hình bên dưới

Kiểm tra ngoài giao diện website:

 

Bài viết liên quan

post-no-image

Chia sẻ một số thư viện loading css đẹp

giai-phap-chinh-css-khong-bi-luu-cache-trinh-duyet

Giải pháp tốt khi chỉnh sửa css mà không sợ bị lưu cache trình duyệt

post-no-image

Một số css tùy biến thẻ input type: select, number

post-no-image

Tắt lazyload cho một số hình ảnh

post-no-image

Tuỳ biến layout YITH WooCommerce Wishlist

post-no-image

Một số hook hoặc filter thường sử dụng trong plugin WPML