[英]Jquery Allow Empty space between Characters only on Key press
Hi how to allow empty characters between words only. 嗨,如何只允许单词之间留空字符。 I have tried but doesn't allow empty character anywhere.
我已经尝试过,但不允许在任何地方使用空字符。
Jquery jQuery的
$('#txtName').keypress(function (e) { var regex = new RegExp("^[a-zA-Z0-9_ ]*$"); var str = String.fromCharCode(!e.charCode ? e.which : e.charCode); if (regex.test(str)) { return true; } else { e.preventDefault(); alert('Please Enter Alphabet'); return false; } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="txtName">
It's for name validation. 它用于名称验证。 Thanks.
谢谢。
Use this pattern: 使用以下模式:
/(\s{2,})|[^a-zA-Z']/g
$('#txtName').keypress(function(e) { var $this = $(this); $(this).val($this.val().replace(/(\\s{2,})|[^a-zA-Z0-9_']/g, ' ').replace(/^\\s*/, '')); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="txtName" />
use Trim()
method, with this method you can remove any space in first and last of your string, then put equal to null
or white-space
Online demo 使用
Trim()
方法,使用此方法,您可以删除字符串的开头和结尾的任何空格,然后将其放置为等于null
或white-space
在线演示
if you want just Regex with /^[\\s\\\xA0]+|[\\s\\\xA0]+$/g
you can get any white-space in first and last of your string then out not equal to null
or white-space
Demo 如果只想使用
/^[\\s\\\xA0]+|[\\s\\\xA0]+$/g
正则表达式,则可以在字符串的/^[\\s\\\xA0]+|[\\s\\\xA0]+$/g
获取任何空格,然后输出不等于null
或white-space
演示
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.