gpt4 book ai didi

JavaScript Onclick 电话/电子邮件验证功能不起作用

转载 作者:行者123 更新时间:2023-12-02 22:47:36 30 4
gpt4 key购买 nike

我有一个搜索字段,我希望用户能够通过电话或电子邮件进行搜索。但是,我的 onclick 事件没有触发。我已经尝试过控制台工具,但似乎无法通过那里进行调试。

<body>  

</div>
<div style="text-align: center;">
{% from "_formhelpers.html" import render_field %}
<form method="POST" action="/">
<dl style="display:inline-block">{{render_field(form.search)}}</dl>
<button id="searchbutton" style="display:inline-block" type="submit" class="btn btn-outline-success my-2 my-sm-0" onclick="doValidate()" >Search</button>

</form>
</div>

<script>

function validateEmail(email) { //Validates the email address
var emailRegex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return emailRegex.test(email);
}

function validatePhone(phone) { //Validates the phone number
var phoneRegex = /^(\+91-|\+91|0)?\d{10}$/; // Change this regex based on requirement
return phoneRegex.test(phone);
}

function doValidate() {
if (!validateEmail(document.appointment.requiredphone.value) || !validatePhone(document.appointment.requiredphone.value) ){
alert("Invalid Email");
return false;
}
}
</script>
{% endblock %}
</html>

最佳答案

该脚本在概念上是有效的,但是由于您没有包含渲染的标记,并且可能不包含一些相关的 JavaScript,因此很难确定您遇到的具体问题。

这个问题几乎肯定与您代码中的 JavaScript 有关:

document.appointment.requiredphone.value

什么是预约?什么是必需电话?如果不知道 HTML 的结构,就不可能确定确切的问题。

由于您仅包含 {{render_field(form.search)}} 而不是渲染的 HTML,因此这是我们所能提供的帮助。

工作片段
(使用 document.getElementById 和一些文本输入)

function validateEmail(email) { //Validates the email address
var emailRegex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return emailRegex.test(email);
}

function validatePhone(phone) { //Validates the phone number
var phoneRegex = /^(\+91-|\+91|0)?\d{10}$/; // Change this regex based on requirement
return phoneRegex.test(phone);
}

function doValidate() {
if (!validateEmail(document.getElementById('email').value) || !validatePhone(document.getElementById('password').value)) {
alert("Invalid Email");
return false;
}
}
<div style="text-align: center;">
<form method="POST" action="/">
<dl style="display:inline-block"><input type="text" id="email"><input type="text" id="password"></dl>
<button id="searchbutton" style="display:inline-block" type="submit" class="btn btn-outline-success my-2 my-sm-0" onclick="doValidate()">Search</button>
</form>
</div>

关于JavaScript Onclick 电话/电子邮件验证功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58326135/

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