使用javascript vs php进行表单验证 [英] form validation with javascript vs php

查看:177
本文介绍了使用javascript vs php进行表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



为什么我需要使用JavaScript进行表单验证,因为用户可能已关闭JavaScript支持,因此我仍然需要使用PHP。 ?



更新:



好的,谢谢你的回答。这听起来像是一个好主意,也可以在客户端使用。我在哪里可以下载好的JavaScript验证?



你知道我在哪里可以下载雅虎那种验证脚本吗?

解决方案

Javascript验证允许您的用户在将表单提交到服务器之前获知任何错误。这可以节省刺激的页面重新加载(因为在提交JS时会捕获事件并验证表单,如果发现错误则阻止表单提交),并最大限度地减少他们不得不重新输入信息的机会(并且一次又一次地... ),或者在正确填写表格之前离开。 JS验证不是服务器端验证的替代品(因为用户可以看到JS,并且通过保存页面和修改JS做他们想做的任何事情);但它对他们来说是一种方便。

这只是渐进式增强概念的一部分,因此JS提供了一种增强用户体验的机制,如果它存在并且打开,并希望使他们与您的网站的互动愉快,或者至少,最低限度的恼人。



编辑 (我倾向于写一个图书馆),但是我不能 - 推荐任何一个图书馆(我倾向于写一个图书馆)我自己的要求,或从以前自我写作的例子借),一个 Google搜索将这些选项向上抛出:


Why should I bother to use JavaScript for form validation when I still have to use PHP since the user could have JavaScript support turned off.

Isn't it unnecessary?

Update:

Ok thanks for your answers. it sounds like a good idea to have it on the client side too. where can I download good JavaScript validations?

Do you know where I can download a validation script like that one in yahoo when you register an account?

解决方案

Javascript validation allows your user to be informed of any errors prior to their submitting the form to the server. This saves irritating page-reloads (since on submit the JS catches the event and validates the form, preventing form-submission if errors are found) and minimises the chances of their having to re-enter information again (and again and again...), or leaving prior to completing the form properly. JS validation is not a substitute for server-side validation (since the user can see the JS, and, by saving the page and amending the JS do whatever they want); but it's a convenience for them.

This is simply part of the concept of progressive enhancement, whereby JS provides a mechanism for enhancing the experience for the user, if it's there and turned on, and hopefully makes their interaction with your site pleasant, or, at least, minimally irritating.


Edited in response to OP's question regarding 'where to download a JS validation tool.'

While I can't -necessarily- recommend any one library (I tend to write my own as required, or borrow from previously self-written examples), a Google search threw these options up:

这篇关于使用javascript vs php进行表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆