0

I wrote a control over some input in my admin form, which is executed on save buttons' click (I control a repetition of some inputs, but that's not important).

So, if this check fails, I alert a message, but I also need to avoid django admin to send this wrong request to the server. How can i do it in Javascript? (i'm using jQuery)

Thorin Schiffer
  • 2,818
  • 4
  • 25
  • 34
exrezzo
  • 497
  • 1
  • 5
  • 24
  • Remember that javascript is executed client-side. Any logic you put in to prevent a button being pressed can be subverted by a suitably determined user, so if the prevention of the request submission is vital to the security/integrity of your application then do not leave it up to javascript to prevent it. – ptr Mar 13 '14 at 14:27
  • @PeteTinkler I know that, but this isn't a very important control, it's only to avoid repetition of records with the same name, besides this is a solution for a webapp for a callcenter, I don't think anyone will try to subvert data :D – exrezzo Mar 14 '14 at 10:31

2 Answers2

1

If you are using jQuery:

$( "form" ).submit(function( event ) {
  event.preventDefault();
  ...
});

http://api.jquery.com/event.preventdefault/

Silwest
  • 1,620
  • 1
  • 15
  • 29
1

If you are not using jQuery:

document.getElementById("id_form").addEventListener("submit", function(event){
    event.preventDefault();
    ...
});

For old browsers look here (second answer).

Community
  • 1
  • 1
isar
  • 1,661
  • 1
  • 20
  • 39