2

I am having a form in which i have applied the javascript validations, but if the user has javascript disabled then those validations does not work, please help me that how can i stop the user from submitting form until & unless he enables the javascript. Thanks

1
  • 4
    You must validate on the server-side as well even if it seems redundant to repeat the validations already coded in JavaScript because a motivated user can find a way to submit the form (e.g., via development tools) whether you like it or not. If you only want to implement validation in one place do it on the server, not the client. Commented Nov 8, 2011 at 7:31

5 Answers 5

10

Use JavaScript to put the submit button there.

<script type="text/javascript">
    document.write("<input type=\"submit\" value=\"Submit Form\" />");
</script>
<noscript>
    <p style="color: red;"><b><i>Please enable JavaScript to continue</i></b><p>
</noscript>
Sign up to request clarification or add additional context in comments.

5 Comments

Does this also prevent submit if you press Enter in one of the form fields?
Of course, because there simply is no submit button if you don't have JavaScript on.
sure it works, but using document.write is discouraged though.
Why? What's the problem with using that in this situation?
In my opinion there's nothing wrong with document.write in this situation. Regarding the Enter key working without a submit button: it will if there is only one field in the form (dating back to early browsers and the HTML 2 spec).
7

You could add the disabled="disabled" attribute on your submit button. Then using javascript when the DOM is loaded remove this attribute.

1 Comment

Even better, create all elements of the form disabled then have Javascript enable them.
1

you cannt, on the client side. But you can still do validation on the server side. Essentially, server-side validation is what really matters.

Comments

0

You can use javascript validation, but note that javascript is just for adding more usability (i.e use it to make form submission easier for the client). Without javascript a form should still be submittable, partially for the small user-base which doesn't have javascript enabled by default.

partially because of a very small user base who otherwise will pwn you in the face for only using client side javascript validation and can (and probably will try at some point) to bypass this. So this is why server validation is a must, not only for the small user base without javascript.

Then ofcourse to answer your question, for the small user-base who wont try to manipulate your database or server you could remove your <form> and use:

<noscript>
  You can FORM_BEHAVIOUR me with javascript enabled.
</noscript>

<script>
  create a form here with javascript
</script>

How exactly you want to render your form is then ofcourse up to you, but note that no javascript can stop the very-small user base from abusing it.

Comments

-2

javascriptdemo.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JAVASCRIPTDEMO</title>
<script language="javascript" type="text/javascript">
function onset()
{

document.forms["jdemoform"].elements["uname"].focus();

}
function jdemo()
{
if(document.forms["jdemoform"].elements["uname"].value=="")
{
    alert("fill your name");
    document.forms["jdemoform"].elements["uname"].focus();
    return false;
}

    document.forms["jdemoform"].submit();

}
</script>
</head>
<body onload="onset();">
if javascript disabled then form will not be submitted
<form name="jdemoform" method="post" action="getname.php">
Name:<input type="text" name="uname" />
<input type="button" value="Submit" onClick="return jdemo();"/>
</body>
</html>

                           getname.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JAVASCRIPTDEMO</title>
</head>
<?php
echo $_POST["uname"];
?>
<body>
</body>
</html>

Comments

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.