How to allow only one space in JavaScript real time filtering -


I am using this custom function below to break unwanted characters from the text field, this is done in real time The problem is that it allows spaces and users can enter as many spaces as they want, but I only allow one place.

Is this possible?

  function mask8 (x) {var string = document getElementById (x) var regex = / [^ A-Za-z] / gi; $ ("#" + X) .removeClass ("Error"); If (string.value.search (regex)> gt; -1) {string.value = string.value.replace (regex, ""); $ ("#" + X) .addClass ("Error"); }}  

If you reduce several places automatically for a single place By creating an error status, you can:

  function mask (x) {var obj = document.getElementById (x); Var val = obj.value; Var illegalities = / [^ azza-jade] / g; $ ("#" + X) .removeClass ("Error"); // Automatically drop many places below one place, // do not make the status of an error val = val.replace (/ \ s + / g, ""); If (val.search (illegal chairs)> gt; -1) {val = val.replace (illegal chairs, ""); $ ("#" + X) .addClass ("Error"); } Obj.value = val; }  

If you want to make multiple locations an error condition, you can:

  function mask (x) {var obj = Document getElementById (x); Var val = obj.value; Var illegalities = / [^ azza-jade] / g; Var invalid chausormultipluspace = / [^ azza-jade] | \ S \ s / gi; $ ("#" + X) .removeClass ("Error"); If (val.search (illegal charsramalt spaces) gt; -1) {obj.value = val.replace (illegal chairs, ""). Replace (/ \ s + / g, ""); $ ("#" + X) .addClass ("Error"); }}  

Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -