• 0

[jQuery] form validation


Question

Is there a light way to validate a text box? most of the examples when searching are extremely bloated, i only have 1 text box i want to return an error if they leave it blank or if they enter anything other than text or numbers.

Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0

Validate upon blur (lose focus)?

1) This is client-side validation only, and then only if JavaScript is supported and enabled.

2) In which cas you could not allow non-alphanumeric input anyway

3) Yes you can check for this.

My way?

Create a class called "inputValidatedAlphanumeric".

Attach a handler to the blur event of that class that checks the content, and adds/removes a style as appropriate

Attach a handler to the keypress event of that class that checks the keypress and allows/disallows input as appropriate

This provides a nice re-usable class you can assign to any input box that wants alphanumeric input only.

Link to comment
Share on other sites

  • 0

if you have a form like;

<form id="theform">
<input type="text" id="validate-me"/>
<input type="submit" value="go"/>
</form>

Something along these lines will check the form when it's submitted;

$(document).ready(function(){

$('#theform').submit(function(){
    if($('#validate-me').val() == ""){
        return false; // Don't submit the form
    }
    return true; // All is OK, submit the form
});

});

That'll only check for 1 field that has the ID 'validate-me' though, so it's not the most portable code :laugh:

Link to comment
Share on other sites

  • 0

works a treat thank you

Apologies if this comes across as patronising, but bear in mind that you still need to do server-side validation. A user with JavaScript disabled (and they do exist) could click your submit button and bypass any JQuery validation!

Link to comment
Share on other sites

  • 0

Apologies if this comes across as patronising, but bear in mind that you still need to do server-side validation. A user with JavaScript disabled (and they do exist) could click your submit button and bypass any JQuery validation!

So the only benefit of using jQuery is to avoid the page reloading? (ie. there is no benefit) - I could do PHP validation and be done with it.

Link to comment
Share on other sites

  • 0

Javascript validation is a good addition for people who do have it enabled, because, as you said, it won't require another page load - so it is often worth doing as well as server-side validation.

Link to comment
Share on other sites

  • 0

So the only benefit of using jQuery is to avoid the page reloading? (ie. there is no benefit) - I could do PHP validation and be done with it.

You could, but I'd still recommend doing it in JavaScript as well, if you can be bothered. JavaScript can provide much better looking validation (fancy fade in effects and so on), as well as (as Quigley Guy said) instant validation for the user. It can be frustrating having to wait for the page to reload to find out you filled in something wrong. Ideally, you'd want both, but if you only have time/patience to do one or the other, always go server-side.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.