表单验证时,需要去除文本框前后的空格才可以正确通过验证。以前看到过一句话:任何设计和代码都要对用户足够宽容。
<input type=
"text"
class
=
"phone"
>
<input type=
"button"
id=
"btn"
value=
"过滤"
>
jQuery - $.trim()
var
tel = $(
'.phone'
).val();
var
result = $.trim(tel);
JS - replace&正则
var
tel = document.querySelector(
'.phone'
).value;
var
result = tel.replace(/^(\s|\u00A0)+|(\s|\u00A0)+$/g,
''
);