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

1
  • 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. Commented Feb 3, 2011 at 11:28

3 Answers 3

1

There's a JQuery plugin called AlphaNumeric...

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

Sign up to request clarification or add additional context in comments.

2 Comments

Can i do the same with validate plugin?
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.
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();
     }
})

7 Comments

does exist option to take every inserted char to input and check if he equal to ' '?
you mean only space not whitespace?
in the input should prevent insert ' '
e.preventDefault() or return false, not both
its like you write 10 times that will also works: e.preventDefault()e.preventDefault()e.preventDefault()e.preventDefault()e.preventDefault()e.preventDefault()e.preventDefault()
|
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.

2 Comments

How I doing that with validate jquery plugin

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.