views:

619

answers:

5

I have a form which needs javascript to be enabled for validation, is there a way to disable submit button when javascript is disabled and warn user to enable it first ?

A: 

Don't define an HTML form action, but rather define only an onSubmit javascript event handler. If javascript is not enabled, the onSubmit will not fire, but since there is no form action, the submit button won't do anything either.

You could also opt to have the HTML form action go to an error page explaining that javascript must be enabled, so that the user has some sort of feedback.

Alternatively you can start with the button disabled (as other posters suggested). If you do that, you should also consider a message on the form indicating why the button is disabled. Remove that message with javascript if it is enabled at the same time you re-enable the button.

Eric J.
A: 

Because disabling a button programmatically depending on the environment and alerting him are both tasks depending on some kind of a scripting language ( like JavaScript ), the answer is no :-/

moritz
Well you could have it disabled by default and then enable it with JavaScript
John Rasch
right! i didn't think of that, but anyways, I'm not a big fan of javascript dependencies...
moritz
+5  A: 

Have it disabled by default and if there is javascript, then enable it.

johnnyArt
+1 this is the easiest and most sensible suggestion.
cletus
Thank you cletus :)
johnnyArt
+10  A: 

Disable it by default and use JavaScript to enable it.

<input type="submit" value="Submit" id="submitBtn" disabled />

<script type="text/javascript">
var _onload = window.onload || function()
{
  document.getElementById('submitBtn').disabled = false;
}

_onload();
</script>

That way, if JavaScript is disabled then the button will also remain disabled.

jQuery version:

<script type="text/javascript">
$(function(){
  $('#submitBtn').attr('disabled', false);
});
</script>
Darrell Brogdon
Thanks for your help :)
Datis
Just be careful with the `_onload` function, because if the `window.onload` property has been previously set, your function will *never* run. Actually, since the `script` block is defined after the input, you can assume that `getElementById` will find it because when the script is evaluated, the input is already in the DOM...
CMS
@CMS Right. Which is why I prefer jQuery.
Darrell Brogdon
A: 

You got solution for enabling part. For Alerting part you can use good old tag. (-:

<noscript>
<H1> It Wont work w/o javascript. Please Enable</h1>
</noscript>
Alexander Taran