j_d
j_d

Reputation: 3082

Safest way to disable submit button?

I'm doing some form validation, and am currently disabling the submit button like this:

$("#button").prop('disabled', true);

I'm wondering what the best practice is for disabling a button, though. Surely there's no point in doing server-side validation via AJAX if you're just disabling a button with js/jQuery - a user could easily un-disable the button in their browser and be on their way.

What's the safest way to go about this?

Upvotes: 0

Views: 351

Answers (2)

BhavO
BhavO

Reputation: 2399

You should always be validating data on the server side.

Ultimately the end user could by pass the browser all together and use fiddler for example, so client side validation is a convenience to improve user experience.

So, disabling the button, should be fine, but also remember that pressing enter/return button in fields also submits forms in most browsers.

The below will prevent submission if some condition you specify is true.

$(document).ready(function(){ 
     $("#form1").submit(function(event){
      if(someCondition)
           event.preventDefault(); 
      });
});

Upvotes: 5

Bhavesh G
Bhavesh G

Reputation: 3028

$("#button").prop('disabled', true); is best way to disable a button at CLIENT SIDE.

Bug again you have to validate all things before processing at backend. Thus you have to apply validation two times,

  1. To display that "something is wrong with input",
  2. To validate all input finally before processing at backend (when user submits form).

Upvotes: 0

Related Questions