jcadima
6/11/2017 - 7:00 PM

Phone number masking

Phone number masking

http://jsfiddle.net/mykisscool/VpNMA/


	jQuery('#phonenumber').keydown(function (e) {
			var key = e.charCode || e.keyCode || 0;
			$phone = jQuery(this);
	
			// Auto-format- do not expose the mask as the user begins to type
			if (key !== 8 && key !== 9) {
				if ($phone.val().length === 4) {
					$phone.val($phone.val() + ')');
				}
				if ($phone.val().length === 5) {
					$phone.val($phone.val() + ' ');
				}			
				if ($phone.val().length === 9) {
					$phone.val($phone.val() + '-');
				}
			}
	
			// Allow numeric (and tab, backspace, delete) keys only
			return (key == 8 || 
					key == 9 ||
					key == 46 ||
					(key >= 48 && key <= 57) ||
					(key >= 96 && key <= 105));	
		})
		
		.bind('focus click', function () {
			$phone = jQuery(this);
			
			if ($phone.val().length === 0) {
				$phone.val('(');
			}
			else {
				var val = $phone.val();
				$phone.val('').val(val); // Ensure cursor remains at the end
			}
		})
		
		.blur(function () {
			$phone = jQuery(this);
			
			if ($phone.val() === '(') {
				$phone.val('');
			}
	});