Javascript check for spaces

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 10:46:11

问题


I have this function but I want to check for spaces only in the front and back, not in the middle before i sent back what can i do with it...

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; 
  }

回答1:


Time for regular expressions.

function startsOrEndsWithWhitespace(str)
{
    return /^\s|\s$/.test(str);
}

Tests:

> /^\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

if i dont wanna accept 1 1 what do i have to change

function containsWhitespace(str)
{
    return /\s/.test(str);
}

Tests:

> /\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



回答2:


For a really simple solution, if you can use jQuery, use jQuery.trim() and compare the trimmed string with the original. If not equal, then there were spaces so the number is invalid.




回答3:


function trim (myString)
{
return myString.replace(/^\s+/g,'').replace(/\s+$/g,'')
} 

source




回答4:


To trim your string you can write something like this, as it's been said before:

function trim(str){
    return str.replace(/^\s+|\s+$/g), '');
}

But why bother?
Want you really want is:

function validateNumeric(str) {
    return !isNaN(parseFloat(str));
}

Note your original code accepts something like "..." or "7.8..9" as being numeric, which is wrong.

Update: kennebec has called my attention to the fact that parseFloat() will ignore trailing garbage at the end of string. So I call your attention to this alternative given in an answer to question "Validate numbers in JavaScript - IsNumeric()":

function isNumber(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
}

(Original credit goes to CMS).




回答5:


function validateNumeric() {
  var val = document.getElementById("tbNumber").value;
  if (!/^\s*(?:\d+(?:\.\d*)?|\.\d+)\s*$/.test(val)) {
    alert('Please enter a valid number');
    return false; 
  }
  return true;
}

(?:\d+(?:\.\d*)|\.\d+) breaks down as follows:

  1. \d+ is any number of digits, e.g. 123
  2. (\.\d*)? optionally matches a fraction, e.g. .25 and . or blank but not .1.2
  3. \.\d+ matches a fraction without an integer part as in .5 but not 1.5.
  4. (?:abc|def) groups things together and matches either abc or def

/^\s*(?:\d+(?:\.\d*)|\.\d+)\s*$/ means any number of spaces followed by one or more decimal digits followed by any number of spaces. So it does what your validChars loop did plus allows spaces at the start and end.



来源:https://stackoverflow.com/questions/6228041/javascript-check-for-spaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!