-3

I have a web form it contains some javascript validtions, but when user explicitly turned off Javascript in the browser, validations are not performed and data submitted by the user gets wrong.

My first concern is that how to prevent the user to submit the form.

I have googled and I got that noscript tag can be used to tell the user that Javascript is disabled in your browser. What I want to ask is that when the user has disabled the Javascript in the browser, can we do any client side validation so user will not submit wrong data.

And can I write code to disable the submit button like

document.getElementById("submit").disabled=true Is this possible.

Any help or suggestion would a great help for me.

4

2 回答 2

3

如果 JavaScript 被禁用,您的客户端验证都不会正常工作。此外,您禁用按钮的代码也将不起作用,因为它是 javascript :(

如果 JavaScript 被禁用,您可以尝试这样的方法来隐藏您的表单并向用户显示消息。

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
.hidden {
    display:none;
}
</style>
</head>

<body>
<noscript>JavaScript is disabled. </noscript>
  <form method="POST" action="http://www.google.com" id="my-form" class="hidden">
     <input type="text" value="Some text here" />

     <input type="submit" value="Submit"  />
  </form>
<script type="text/javascript">
   document.getElementById("my-form").style.display = "block";
</script>
</body>
</html>

​或者您可以执行任何这些方法将用户重定向到另一个页面。

<noscript>
  <meta http-equiv="refresh" content="0;url=your-noscript-page.html">
</noscript>

或这个

<noscript>
  <a href="your-noscript-page.html">JavaScript is disabled in your browser. Click here to continue</a>
</noscript>

或不使用 Modernizr 库的 Modernizr 方法。

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
.no-js form {
    display:none;
}
.js-disabled {
    display:none;
}
.no-js .js-disabled {
    display:block;
}
</style>
</head>

<body class="no-js">
<div class="js-disabled">JavaScript is disabled in your browser.</div>
<form method="POST" action="http://www.google.com" id="my-form">
    <input type="text" value="Some text here" />
    <input type="submit" value="Submit"  />
</form>
<script type="text/javascript">
document.getElementsByTagName("body")[0].className = "";
</script>
</body>
</html>
于 2012-07-19T05:10:51.907 回答
2

简短的回答:没有。长答案:您应该始终在服务器上进行验证,即使您已经(或者您认为)在客户端进行了验证。期望所有用户都是恶意用户总是最安全的,并将“好”用户视为例外。

于 2012-07-19T05:14:07.927 回答