我可以使用哪些JavaScript函数来检查电子邮件和电话号码是否有效,而无需使用jquery插件? [英] What JavaScript functions exist that I can use to check that an email and phone # are valid without resorting to a jquery plugin?

查看:61
本文介绍了我可以使用哪些JavaScript函数来检查电子邮件和电话号码是否有效,而无需使用jquery插件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在表格上,我需要确保所有字段均已填写,并且电话号码和电子邮件地址有效.我尝试使用jQuery验证插件,但它改变了页面的外观.该插件还动态地在意外的地方寻找一些css文件.

On a form, I need to make sure that all fields are filled in and that the phone # and email address are valid. I tried using a jQuery validation plugin but it changed the page's look and feel. The plugin also was dynamically looking for some css files in some spot that was unexpected.

我喜欢jQuery,但对于我想要的插件来说似乎太多了.

I love jQuery but the plugin seemed too much for what I wanted.

由于我要做的就是确保字段不为空并且电话号码有效且电子邮件有效,您建议使用哪些JavaScript函数?我仍将使用jQuery core.

Since all I need to do is to make sure the fields are not empty and that the phone number is valid and email is valid, what javascript functions do you suggest? I will still use jQuery core.

在服务器端,我们要使用Apache Commons PhoneNumberFormatter以及与电子邮件验证相同的邮件.

Serverside we want to use apache commons PhoneNumberFormatter and same with email validation.

谢谢!

推荐答案

我认为您正在寻找 JavaScript正则表达式,使用JavaScript的标准部分RegReg对象.您可以使用它来对电子邮件地址和电话号码进行基本检查.

I think you're looking for JavaScript regular expressions, using the RegExp object that comes as a standard part of JavaScript. You can use that to perform basic checking of email addresses and phone numbers.

例如

function emailIsValid(emailAddress) {
  var emailRegex = /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/;
  return !!emailAddress.match(emailRegex);
}

上面的代码未经测试,但是应该可以使您了解如何执行此操作.再次对电话号码进行相同的操作,然后执行以下操作:

The code above is not tested, but it should give you an idea of how to do it. Just do the same again for the telephone number, and then do something like this:

if (emailIsValid(emailAddressValue) && telephoneNumberIsValid(telephoneValue)) {
  //Submit form
} else {
  alert ("There are errors on the form, please correct and invalid data");
}

这篇关于我可以使用哪些JavaScript函数来检查电子邮件和电话号码是否有效,而无需使用jquery插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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