有关 if 语句的帮助 - 最少 3 个字符

发布于 2024-10-13 07:45:11 字数 254 浏览 3 评论 0原文

由于输入没有 minlength="" ,我如何制作一个 if 语句来执行以下操作:

如果用户输入的字符数不超过 3 个,则不会将其保存在数据库中。

这是输入:

 QTY<input title="QUANTITY PER ITEM" size="1"  name="inputQTY" value="**(minimun of 3 characters)**"/>

有什么想法吗?

Since there is no minlength="" for input, how can I make an if statement that does the following:

If the user has not input more than 3 characters it will not be saved in a database.

Here is the input:

 QTY<input title="QUANTITY PER ITEM" size="1"  name="inputQTY" value="**(minimun of 3 characters)**"/>

any ideas??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不再见 2024-10-20 07:45:11

注意:输入字段是否有最小值限制并不重要;用户仍然可以欺骗表单以包含他或她想要的任何值。因此,您需要在服务器端进行验证。

假设您使用 PHP 作为服务器端语言。

$inputQTY = $_POST['inputQTY'];
if (strlen($inputQTY) < 3) {
   // DO NOT INSERT INTO DB
}
else {
   // insert after further scrubbing input
}

阅读此内容: 清理用户输入的最佳方法是什么PHP?

Note: it doesn't matter if there's a min value restriction on the input field or not; a user can still spoof the form to contain whatever value he or she wants. Thus, you need to validate on the server side.

Let's say you're using PHP as your server side language.

$inputQTY = $_POST['inputQTY'];
if (strlen($inputQTY) < 3) {
   // DO NOT INSERT INTO DB
}
else {
   // insert after further scrubbing input
}

Read this: What's the best method for sanitizing user input with PHP?

忱杏 2024-10-20 07:45:11

表单验证是检查表单字段是否以正确格式填写(如果是进程)的过程。在你的问题中,正确的格式是“超过3个字符”。当然,表单验证的时候会涉及到if语句。在客户端,您使用 JavaScript 代码来验证表单字段。

它的工作原理如下:表单有一个 onsubmit 事件处理程序,当用户单击 Submit 或按 Enter 键时,onsubmit 将被触发。 onsubmit 是放置表单验证代码的位置。

<script>
function onsubmit() {
  if (document.forms.mainForm.inputQTY.value.length < 3) {
    alert("ERROR: Must be more than 3 characters");
    return false; // stops the form submission
  }
  return true;
}
</script>

下面是添加 onsubmit 处理程序的方法

...

onsubmit 函数可以返回一个值: true 允许提交表单,或 false 停止提交。

什么是 document.forms.mainForm.inputQTY.value.length? document.forms.mainForm 引用表单 named mainForm,而 .inputQTY 查找 inputQTY 字段。
您可以使用 document.getElementsByTagName 手动查找这些元素,但编写此类代码非常耗费[时间和空间]。

因为有些用户禁用了JavaScript,或者破解者故意禁用JS来绕过验证,所以必须在服务器端进行验证。

服务器端代码将是这样的:

if (strlen($_GET['inputQTY']) < 3) {
  echo "ERROR: Must be more than 3 characters";
} else {
  // submit data
}

注意:从头编写的代码,未经测试

Form validation is the process of checking if the form fields are filled in correct formats if they are processes. In your question, the correct format is "more than 3 characters". Of course, if statements are involved during form validations. In the client-side, you use JavaScript code to validate form fields.

Here is how it works: A form has an onsubmit event handler, when the user clicks Submit, or presses Enter, onsubmit will be triggered. onsubmit is where you put the form validation code.

<script>
function onsubmit() {
  if (document.forms.mainForm.inputQTY.value.length < 3) {
    alert("ERROR: Must be more than 3 characters");
    return false; // stops the form submission
  }
  return true;
}
</script>

And here is how you add the onsubmit handler

...

The onsubmit function can return a value: true to let the form to be submitted, or false to stop the submission.

What is document.forms.mainForm.inputQTY.value.length? document.forms.mainForm references to the form named mainForm, while .inputQTY finds the inputQTY field.
You can use document.getElementsByTagName to manually find these elements, but it is [time and space]-consuming to write that type of code.

Because some users have JavaScript disabled, or crackers intentionally disable JS to bypass validation, you must validate server-side.

Server-side code will be something like this:

if (strlen($_GET['inputQTY']) < 3) {
  echo "ERROR: Must be more than 3 characters";
} else {
  // submit data
}

NOTE: code written from head, not tested

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文