gpt4 book ai didi

javascript - 表单验证 - 电子邮件/电话号码

转载 作者:行者123 更新时间:2023-11-30 10:36:40 28 4
gpt4 key购买 nike

我的验证函数有问题,我只在一个函数中实现了它,我想添加电子邮件验证,例如 @ 和数字 1-9。如何添加?

html:

<form onsubmit="return validate();" name="formValidation">
<label>First Name:</label>
<input type="text" name="firstName" /><br /><br />
<label>Last Name:</label>
<input type="text" name="lastName" /><br /><br />
<label>E_mail:</label>
<input type="text" name="Email" /><br /><br />
<label>Confirm E_mail:</label>
<input type="text" name="confirmEmail" /><br /><br />
<label>Address:</label>
<input type="text" name="Address" /><br /><br />
<label>Telephone nr:</label>
<input type="text" name="telephoneNr" /><br /><br />
<br />
<p>submit your form: </p><input type="submit" value="Submit" />
</form>

js:

function validate(){
if(document.formValidation.firstName.value == "" ||
document.formValidation.lastName.value == "" ||
document.formValidation.Email.value == "" ||
document.formValidation.confirmEmail.value == "" ||
document.formValidation.Address.value == "" ||
document.formValidation.telephoneNr.value == "")
{
alert("Please fill all the boxes before submitting!");
return false;
} else {
alert('Your form has been submitted!');
}

}

最佳答案

来自 http://www.w3resource.com/javascript/form/email-validation.php :

此函数将使用 javascript 验证电子邮件:

function ValidateEmail(mail)   
{
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(mail))
{
return (true)
}
alert("You have entered an invalid email address!")
return (false)
}

验证电话号码:

function validatePhone(phone) {
var error = "";
var stripped = phone.replace(/[\(\)\.\-\ ]/g, '');

if (stripped == "") {
error = "You didn't enter a phone number.";
} else if (isNaN(parseInt(stripped))) {
phone = "";
error = "The phone number contains illegal characters.";

} else if (!(stripped.length == 10)) {
phone = "";
error = "The phone number is the wrong length. Make sure you included an area code.\n";
}
}

关于javascript - 表单验证 - 电子邮件/电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13628995/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com