Hạn chế ký tự đặc biệt trong textbox javascript

Bạn có muốn hạn chế một số ký tự đặc biệt khỏi trường biểu mẫu không? . Trong hướng dẫn này, chúng tôi sẽ chỉ cho bạn cách hạn chế các ký tự đó bằng Javascript

Thêm JavaScript

Trước tiên, bạn sẽ muốn sao chép đoạn mã này vào trang web của mình. Có hai đoạn dưới đây, một đoạn dành cho các trường biểu mẫu nhập liệu cơ bản và đoạn thứ hai dành cho trường biểu mẫu Văn bản Đoạn văn

Nếu bạn cần bất kỳ trợ giúp nào về cách thêm đoạn trích vào trang web của mình, vui lòng xem lại hướng dẫn này

/**
 * Restrict special characters from forms fields with special CSS class
 * Apply the class "wpf-char-restrict" to the field to enable.
 *
 * @link //wpforms.com/developers/how-to-restrict-special-characters-from-a-form-field/
 */
 
function wpf_dev_char_restrict[] {
?>


	
	jQuery[function[$]{
		$[ '.wpf-char-restrict' ].on[ 'keypress', function[e]{
			
			var regex = new RegExp[ "@" ];
			var key = String.fromCharCode[!event.charCode ? event.which : event.charCode];
			
			if [!regex.test[key]] {
				alert [ "Please fill out the form in English. Thank you!" ]; // Put any message here
				e.preventDefault[];
				return false;
			}
		}];
		
		//Prevent any copy and paste features to by-pass the restrictions
		$[ '.wpf-char-restrict' ].bind[ 'copy paste', function [e] {
		    var regex = new RegExp[ "@" ];
			
			var key = String.fromCharCode[!event.charCode ? event.which : event.charCode];
			
			if [!regex.test[key]] {
				alert [ "Pasting feature has been disabled for this field" ]; // Put any message here
				e.preventDefault[];
				return false;
			}
		}];
		
	}];






	
	jQuery[function[$]{
		$[ '.wpf-char-restrict' ].on[ 'keypress', function[e]{
			
			var regex = new RegExp['^[a-zA-Z]+$'];
			var key = String.fromCharCode[!event.charCode ? event.which : event.charCode];
			
			if [!regex.test[key]] {
				alert [ "Please fill out the form in English. Thank you!" ]; // Put any message here
				e.preventDefault[];
				return false;
			}
		}];
		
		//Prevent any copy and paste features to by-pass the restrictions
		$[ '.wpf-char-restrict' ].bind[ 'copy paste', function [e] {
			
		    var regex = new RegExp['^[a-zA-Z]+$'];
			var key = String.fromCharCode[!event.charCode ? event.which : event.charCode];
			
			if [!regex.test[key]] {
				alert [ "Pasting feature has been disabled for this field" ]; // Put any message here
				e.preventDefault[];
				return false;
			}
		}];
		
	}];



Chủ Đề