热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在JavaScript中实现电子邮件和密码的输入验证-ImplementingInputValidationforEmailandPasswordinJavaScript

本文旨在构建一个JavaScript函数,用于对用户输入的电子邮件地址和密码进行有效性验证。该函数将确保输入符合标准格式,并检查密码强度,以提升用户账户的安全性。通过集成正则表达式和条件判断语句,该方法能够有效防止常见的输入错误,同时提供即时反馈,改善用户体验。

What I want to do is establish a function in Javascript that will validate an inputted email and password, that checks the inputted email to make sure it has at least 2-3 characters after the last period in the string (for .com, .org, .ca, etc.) and that the string has at least one '@' symbol in it.

我想要做的是建立一个在Javascript函数,验证输入电子邮件和密码,检查输入电子邮件,以确保它至少有2 - 3字符字符串中的最后一段时间后(. com,.org,.ca等等),至少有一个“@”符号的字符串。

As for checking the password, I want the function to check that it has at least one lowercase and one uppercase letter, at least one number, and at least one special character (!,@,#,$,%,^,&,*,~)

至于检查密码,我希望功能检查,至少一个小写字母,一个大写字母,至少一个数字,至少一个特殊字符(! @、# $,%,^,&、* ~)

Does anyone know what I would have to do to get the regular expression to check the password for at least 1 special character like !, @, *, etc.?

有人知道我要做什么才能让正则表达式检查至少一个特殊字符的密码,比如!,@,*等等?

Here's what I have:

这就是我有:

function validatePassword(password)
{
    var passwordPattern = /(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}/;
return passwordPattern.test(password)
}

// Validate form
function validate()
{
var email = user.email.value;
if(validateEmail(user.email.value))
    user.validEmail.value = "OK";
else
    user.validEmail.value = "X";
if(validatePassword(user.password.value))
    user.validPassword.text = "OK";
else
    user.validPassword.text = "X";
}

3 个解决方案

#1


2  

You can user Regular Expressions for both cases. As for the e-mail, you can read more here: http://www.zparacha.com/validate-email-address-using-Javascript-regular-expression/ As for the password check, I'd recommend reading this: http://www.the-art-of-web.com/Javascript/validate-password/

您可以对这两种情况使用正则表达式。至于电子邮件,您可以在这里阅读更多内容:http://www.z伞兵a.com/validate-email-address- Javascript-正则表达式/至于密码检查,我建议您阅读以下内容:http://www.the-art of-web.com/Javascript/validate-password/

Here's the validation of password:

密码验证如下:




Note: This regular expression doesn't check for special characters. I think it's kind of specific to your definitions, but you can edit the expression and add a list of characters that you'd consider special for your case.

注意:这个正则表达式不检查特殊字符。我认为它是特定于你的定义的,但是你可以编辑表达式并添加一个你认为适合你的情况的字符列表。

#2


1  

Please see my project of the cross-browser filter of value of the text input element on your web page using Javascript language: Input Key Filter . You can filter the value as an integer number, a float number, or write a custom filter, such as a phone number filter. See an example of code of input an email and password:

请参阅我的项目,跨浏览器过滤器的价值文本输入元素在您的网页上使用Javascript语言:输入关键字过滤器。您可以将值筛选为整数、浮点数或编写自定义过滤器(如电话号码过滤器)。参见输入电子邮件和密码的代码示例:




    
	
	
	
	
	
	
			
	
	
	


Email: 

 New email: 

Password: New password:

Also see my page "Email:" and "Password:" of the example of the input key filter

还可以查看我的页面“Email:”和“Password:”的输入密钥过滤器示例

#3


0  

You could use regexp for both of these

这两个都可以使用regexp。

function validate(form_id,email) {

   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = document.forms[form_id].elements[email].value;
   if(reg.test(address) == false) {

      alert('Invalid Email Address');
      return false;
   }
}

for the email (source: http://www.white-hat-web-design.co.uk/blog/Javascript-validation/)

用于电子邮件(来源:http://www.white hat-web-design.co.uk/blog/Javascript-validation/)

And use the same method for verifying your Password.

使用相同的方法验证密码。

http://www.w3schools.com/jsref/jsref_regexp_test.asp

http://www.w3schools.com/jsref/jsref_regexp_test.asp


推荐阅读
author-avatar
哎哟不错哦凉皮
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有