我有这个功能,但我想在前面和后面检查空格,而不是在中间我发回来之前我可以用它做什么...
function validateNumeric() {
var val = document.getElementById("tbNumber").value;
var validChars = '0123456789.';
for(var i = 0; i < val.length; i++){
if(validChars.indexOf(val.charAt(i)) == -1){
alert('Please enter valid number');
return false;
}
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
正则表达式的时间.
function startsOrEndsWithWhitespace(str)
{
return /^\s|\s$/.test(str);
}
Run Code Online (Sandbox Code Playgroud)
测试:
> /^\s|\s$/.test('123454')
false
> /^\s|\s$/.test('123 454')
false
> /^\s|\s$/.test(' 123454')
true
> /^\s|\s$/.test(' 123454 ')
true
> /^\s|\s$/.test('123454 ')
true
Run Code Online (Sandbox Code Playgroud)
如果我不想接受1 1我必须改变什么
function containsWhitespace(str)
{
return /\s/.test(str);
}
Run Code Online (Sandbox Code Playgroud)
测试:
> /\s/.test('123454')
false
> /\s/.test('123 454')
true
> /\s/.test(' 123454')
true
> /\s/.test('123454 ')
true
> /\s/.test(' 123454 ')
true
> /\s/.test(' 123 454 ')
true
Run Code Online (Sandbox Code Playgroud)