如何通过 JavaScript 验证此表单?
我用 HTML/CSS/PHP 完成了一个简单的、基于国际象棋的业余爱好项目。它根据给定棋子的类型、位置和颜色列出并显示其可能的移动。不过,该程序不考虑棋盘上的任何其他棋子。
如您所见,该表单工作正常并且具有服务器端验证。我的问题是我对 JavaScript 不够熟悉,无法从头开始翻译表单的两个文本字段的服务器端验证[为了清晰起见进行了编辑]。有人可以帮助在 JavaScript 中复制我的 PHP 验证代码两个输入吗?我不想使用任何框架,因为我想最终精通 JS。
- 以下是该表单的 PHP 代码: http://pastebin.com/embed_js.php?i=0LRJxkFg
- 下面是用于处理的 PHP 代码(参见第 76-120 行)http://pastebin. com/embed_js.php?i=pksn9xPd
感谢任何帮助。
I've done a simple, chess-based hobby project with HTML/CSS/PHP. It lists and displays the possible moves for a given piece based its type, location, and colour. The program doesn't consider any other pieces on the board, though.
As you can see, the form works fine and has server-side validation. My problem is I'm not familiar enough with JavaScript to translate the server-side validation [edited for clarity] of the form's two text fields from scratch. Could somebody help replicate my PHP-validation code for the two inputs in JavaScript? I don't want to use any frameworks because I want to be ultimately be well-versed in JS.
- Here's the PHP code for the form: http://pastebin.com/embed_js.php?i=0LRJxkFg
- Here's the PHP code for processing (see lines 76-120) http://pastebin.com/embed_js.php?i=pksn9xPd
Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,这是第一个字段的验证代码。 (即兴写下,在我的脑海中它可以工作,但现实生活有时很糟糕。而且我现在已经习惯使用 jQuery,很难回到基础知识。)
这样调用它:
你必须在提交表单的处理程序。这是处理程序:
并更改表单元素的定义以在提交时调用它:
我将错误消息的显示留给读者作为一个简单的练习。
OK, here's the validation code for the first field. (Written off the cuff, in my head it works but real life sucks sometimes. Plus I'm so used to using jQuery now, it's hard to go back to basics.)
Call it like this:
You will have to call this in a submit handler for the form. Here's the handler:
And change the definition of the form element to call this on submit:
I leave the display of error messages as an easy exercise for the reader.