JavaScript 的 IF 语句
我正在查看 Jquery UI 的 模态表单演示。但我想补充一些东西。 该演示检查所有输入(姓名、电子邮件、密码)长度并验证它们。但电子邮件输入必须是可选的。因此,我将电子邮件输入的最小值(对于 checkLength() 函数)设置为零。但是当我留下空白电子邮件输入时,我收到验证错误。您可以在此处查看完整代码。
这是带有此代码的验证电子邮件:
bValid = bValid && checkRegexp( email, ..SOME REGEX PATTERNS WILL COME HERE.., "eg. [email protected]" );
但我只想在有任何电子邮件输入时尝试验证。因为电子邮件输入是可选的,访客可以留下它。我如何向此代码添加 if 语句(如果电子邮件输入已填写,请验证它。否则不要尝试验证)? 我希望一切都清楚。
I'm looking Jquery UI's modal form demo. But i want to add something.
This demo checking all inputs' (name,email,password) lenght and validate them. But email input must be optional. Because of this i'm setting email input's min value (for checkLength() function) to zero. But when i leave blank email input i'm getting validation error. You can check full codes here .
It's validation email with this code :
bValid = bValid && checkRegexp( email, ..SOME REGEX PATTERNS WILL COME HERE.., "eg. [email protected]" );
But i want to try validation only if there is any email input. Because email input is optional and visitors can leave it. How can i add if statement (if email input is filled, validate it. Else don't try validation) to this code ?
I hope it's clear.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你说“只有当电子邮件不为空时如何进行验证?”,那么我认为这没有理由不起作用:
注意:如果你实际上不知道如何在 JS 中使用条件语句,你可能有一个 <你面前有很多障碍。请参阅此以获取快速参考,和/或获取一本有关 JS 基础知识的书籍。
If you are saying "how to do validation only if email is not empty?", then I see no reason this wouldn't work:
Note: if you actually have no idea how to use conditional statements in JS, you may have a lot of hurdles ahead of you. See this for a quick reference, and/or get a book about JS basics.