gpt4 book ai didi

JavaScript 表单验证

转载 作者:行者123 更新时间:2023-12-02 14:15:59 25 4
gpt4 key购买 nike

我正在对必填字段使用 JavaScript 验证。这是我的 html

<form class="uk-form-stacked" name="myForm"  action="<?php echo base_url(); ?>admin/pages/create_service"  id="wizard_advanced_form" method="post" enctype="multipart/form-data" onsubmit="return myFunction(this)" novalidate>
<div data-uk-grid-margin="" class="uk-grid">
<div class="uk-width-medium-1-2">
<label for="service_title">Service Title<span class="req">*</span></label>
<input type="text" name="service_title" id="validd" class="md-input" />
<p id="demo"></p>
</div>
</div>
<div class="uk-grid">
<button type="submit" class="md-btn md-btn-primary md-btn-wave-light waves-effect waves-button waves-light" >Submit</button>
</div>
</form>

我的 JavaScript 是

    <script>
function myFunction(form) {
var x, text;
x = document.getElementById("validd").value;
if (x == null || x == "") {
text = "Input not valid";

}
document.getElementById("demo").innerHTML = text;
return false;

}
</script>

现在,当我的输入字段为空并且我提交表单时,它显示我的输入无效,这很好。但即使当我填写一些文本输入时,它也会显示未定义,而不是输入无效,而不是提交表单。请帮忙..

最佳答案

您忘记添加 else,如果条件不满足,它将返回 true

 <script>
function myFunction(form) {
var x, text;
x = document.getElementById("validd").value;
if (x == null || x == "") {
text = "Input not valid";
document.getElementById("demo").innerHTML = text;
return false;
}
else{
return true;
}
}
</script>

关于JavaScript 表单验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39031566/

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