See if Input Has Spaces with jQuery

后端 未结 2 946
陌清茗
陌清茗 2021-01-19 23:13

I\'m trying to write a function that reads a inputs value, and determines if a space has been used in it\'s creation. I\'m not trying to trim anything -- just see if it woul

相关标签:
2条回答
  • 2021-01-19 23:39

    Use val and indexOf :

    var hasSpace = $('#myInputId').val().indexOf(' ')>=0;
    

    If you want to test other types of "spaces" (for example a tabulation), you might use a regex :

    var hasSpace = /\s/g.test($('#myInputId').val());
    

    Demonstration

    0 讨论(0)
  • 2021-01-19 23:42

    Use contains for include space:

    var value = $('#myInputId').val();
    if(value.contains(' ')){
       console.log("has space");
    }
    

    Or you can find with these ascII code &nbsp and \xA0

    if(value.contains(' ')){
       console.log("has space");
    }
    
    0 讨论(0)
提交回复
热议问题