0

How avoid from user insert white-spaces in ? (I use jquery.validate.js plugin)

Example:

input not possible(because user not able insert white-spaces): John Pitt My

input possible:JohnPittMy

Edit:

  1. use of regex in this case mistake - should be used only value.indexOf(' ')

  2. please I asking how to that with only http://docs.jquery.com/Plugins/Validation

Thanks

Ben
  • 25,389
  • 34
  • 109
  • 165
  • with the indexof there is a problem that the keydown is called when the key is pressed so you cant check that the .value contains a bad char or not, and if you trying to check with the keyup event, you cant interrupt the key pressing. – Gergely Fehérvári Feb 03 '11 at 11:28

3 Answers3

1
if(value.match(/^([^\s])*$/)) {return true;}

hope this good.not tested. mut mainly you should use regular expression.

preventing insert spaces:

$("#username").keypress(function(e) {
     if(e.which == 32) {
        e.preventDefault();
     }
})
Gergely Fehérvári
  • 7,811
  • 6
  • 47
  • 74
1

There's a JQuery plugin called AlphaNumeric...

See: http://www.itgroup.com.ph/alphanumeric/

John K.
  • 5,426
  • 1
  • 21
  • 20
  • Can i do the same with validate plugin? – Ben Feb 01 '11 at 14:24
  • You can actually do it with just JQuery. The unpacked version of the plugin is about 80 lines long with just 3 or 4 main functions... pretty simple if you'd like to look at the js and see how Paulo Marinas did it. – John K. Feb 01 '11 at 14:38
0

I'll try to use a regular expression to validate the input.

A regular expression like: /([a-z]*[A-Z]+)+|([a-z]+[A-Z]*)+/ (only letters upper and lower case)

I don't know exactly how to express this regular expression.

But what you need it is any to test your input value that doesn't allow whitespaces.

Fran Verona
  • 5,438
  • 6
  • 46
  • 85
  • How I doing that with validate jquery plugin – Ben Feb 01 '11 at 14:30
  • I hope this can help you http://stackoverflow.com/questions/280759/jquery-validate-how-to-add-a-rule-for-regular-expression-validation/709358#709358 – Fran Verona Feb 01 '11 at 14:32