有关 if 语句的帮助 - 最少 3 个字符
由于输入没有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
注意:输入字段是否有最小值限制并不重要;用户仍然可以欺骗表单以包含他或她想要的任何值。因此,您需要在服务器端进行验证。
假设您使用 PHP 作为服务器端语言。
阅读此内容: 清理用户输入的最佳方法是什么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.
Read this: What's the best method for sanitizing user input with PHP?
表单验证是检查表单字段是否以正确格式填写(如果是进程)的过程。在你的问题中,正确的格式是“超过3个字符”。当然,表单验证的时候会涉及到if语句。在客户端,您使用 JavaScript 代码来验证表单字段。
它的工作原理如下:表单有一个
onsubmit
事件处理程序,当用户单击 Submit 或按 Enter 键时,onsubmit
将被触发。onsubmit
是放置表单验证代码的位置。下面是添加 onsubmit 处理程序的方法
...
onsubmit
函数可以返回一个值: true 允许提交表单,或 false 停止提交。什么是 document.forms.mainForm.inputQTY.value.length?
document.forms.mainForm
引用表单name
dmainForm
,而.inputQTY
查找inputQTY
字段。您可以使用 document.getElementsByTagName 手动查找这些元素,但编写此类代码非常耗费[时间和空间]。
因为有些用户禁用了JavaScript,或者破解者故意禁用JS来绕过验证,所以必须在服务器端进行验证。
服务器端代码将是这样的:
注意:从头编写的代码,未经测试
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.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 formname
dmainForm
, while.inputQTY
finds theinputQTY
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:
NOTE: code written from head, not tested