如何退出表单验证函数,以便不提交表单

编程入门 行业动态 更新时间:2024-10-27 08:38:40
本文介绍了如何退出表单验证函数,以便不提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

HI, 只是一个警告,我是一个javascript新手。我正在编写一个函数来支持在我正在开发的网页上验证表单的内容。由于 我是新手,这个功能在这个时候非常简单(事实上,我不知道是否b $ b甚至不知道它是否完全有用,我''我还在调试)。但是, 第一个问题是当遇到错误时,我收到警报 框,我按ok然后提交表单并且新数据是 输入数据库(它不应该)。基本上,这是 我得到的(没有所有的表单标签): 在<页面的头部: < script type =" text / javascript"> function CheckFormValues(){ var nullsPresent = false; var npf = document.getElementById(" NewPatientForm"); for(var i = 0; i< npf.length; i ++){ if (npf.elements [i] .nodeType ==" textarea") 继续; if(npf.elements [i] .value == "{ nullsPresent = true; 休息; } } if(nullsPresent){ alert("此表单上的任何项目都不能为空(评论除外) \ n请正确并重新提交);; 返回; } npf.submit(); } < / script> 然后,提交按钮(在表单标签中): < input type = button onClick =" ; CheckFormVa梅毒()" style =" height:40px; font- size:18px;" value ="提交新患者 /> 我需要在函数中使这个函数退出以便 用户填写空字符串然后重新提交?你可以看到,b $ b,我尝试了一个返回。在调用nfp.submit()之前,那个 没有用。它仍然将数据提交给数据库。 谢谢, Andy

解决方案

在2008-11-17 01:49,Andrew Falanga写道:

只是一个警告,我是一个javascript新手。

没问题。我在括号中提出了一些建议。

第一个问题是当遇到错误时,我收到警报 框,我按确定,然后提交表格,新数据是 输入数据库(它不应该)。

...

function CheckFormValues(){

(除非该函数是作为构造函数,否则在JS中使用 小写字母来启动函数名称时,这被认为是好的风格。 var nullsPresent = false; var npf = document.getElementById(" NewPatientForm"); for(var i = 0; i< npf.length; i ++){

(这将在每次迭代时评估npf.length。因为长度是 常数,这可以更有效地写成: for(var i = 0,len = npf.length; i< len; i ++) )

if(npf.elements [i] .nodeType ==" textarea") 继续; if(npf.elements [i] .value ==""){

(是单个空格认为有效输入?)

然后,提交按钮(在表单标签中): < input type = button onClick =" ; CheckFormValues()" style =" height:40px; font- size:18px;" value ="提交新患者 />

(这不是真正的提交按钮(type =" submit"))。 你可以避免按钮的默认操作,如下所示: < input type =" submit" onclick =" return CheckFormValues()" ...> 在验证函数中,返回true / false。您可能还想直接传递 表单元素;这将为你节省getElementById()调用: 函数CheckFormValues(npr){... .. < ; input ... onclick =" return CheckFormValues(this.form)" ...> HTH。 - 康拉德

" Andrew Falanga" < af ****** @ gmail写信息 新闻:5a ************************ ********** @ u18g2000 pro.googlegroups ...

HI, 只是一个警告,我是一个javascript新手。我正在编写一个函数来支持在我正在开发的网页上验证表单的内容。由于 我是新手,这个功能在这个时候非常简单(事实上,我不知道是否b $ b甚至不知道它是否完全有用,我''我还在调试)。但是, 第一个问题是当遇到错误时,我收到警报 框,我按ok然后提交表单并且新数据是 输入数据库(它不应该)。基本上,这是 我得到的(没有所有的表单标签): 在<页面的头部: < script type =" text / javascript"> function CheckFormValues(){ var nullsPresent = false; var npf = document.getElementById(" NewPatientForm"); for(var i = 0; i< npf.length; i ++){ if (npf.elements [i] .nodeType ==" textarea") 继续; if(npf.elements [i] .value == "{ nullsPresent = true; 休息; } } if(nullsPresent){ alert("此表单上的任何项目都不能为空(评论除外) \ n请正确并重新提交);; 返回; } npf.submit(); } < / script> 然后,提交按钮(在表单标签中): < input type = button onClick =" CheckFormValues()" style =" height:40px; font- size:18px;" value ="提交新患者 /> 我需要在函数中使这个函数退出所以 用户填写空字符串然后重新提交?你可以看到,b $ b,我尝试了一个返回。在调用nfp.submit()之前,那个 没有用。它仍然将数据提交给数据库。 谢谢, Andy

嗨安德鲁 我没问题。我已经学会了如何使用JS,但作为一名程序员,我从来没有用b $ b来掌握面向对象的语言,所以我还在学习 我以为关于确认和提示框参考: http://www.w3schools / js / js_examples_3.asp 窗口对象 但是,也许你可以尝试(未测试) < script type =" ; text / javascript"> var npf = document.getElementById(" NewPatientForm"); // npf是全局的 函数CheckFormValues(){ var nullsPresent = false; for(var i = 0; i< npf .length; i ++){ if(npf.elements [i] .nodeType ==" textarea") 继续; if(npf.elements [i] .value ==""){ nullsPresent = true; break; } } 返回nullsPresent; } if(CheckFormValues()){ //函数返回true alert("此表单上的任何项目都不能为空(评论除外) \ n请正确并重新提交); CheckFormValues(); } else // OK npf.submit(); < / script> 这个函数甚至可能像这样工作 函数CheckFormValues(){ for(var i = 0; i< npf.length; i ++){ if(npf.elements [i] .nodeType ==" textarea"&& npf.elements [i] .value =="") 返回true; } 返回false; } - Trevor Lawrence 堪培拉 网站 trevorl.mvps

他说的话。 也就是说,我给出了一个应该有效的解决方案,加上康拉德的积分 - Trevor Lawrence 堪培拉 网站 trevorl.mvps " Conrad Lender" < cr ****** @ yahoo写了留言 新闻:eu ************************ ******@supernews.co m ...

On 2008-11-17 01:49,Andrew Falanga写道:

>只是警告,我是javascript新手。

没问题。我在括号中提出了一些建议。

>第一个问题是,当遇到错误时,我收到警报框,我按确定然后提交表单并将新数据输入数据库(它不应该)。

..

> function CheckFormValues(){

(在JS中,用一个 小写字母来启动函数名称被认为是好的风格,除非该函数是用作构造函数)

> var nullsPresent = false; var npf = document.getElementById(" NewPatientForm"); for(var i = 0; i< npf.length; i ++){ (这将在每次迭代时评估npf.length。由于长度为 常数,因此可以更有效地编写为: for(var i = 0,len = npf.length; i< len; i ++) )

> if(npf.elements [i] .nodeType ==" textarea")继续; if(npf.elements [i] .value ==""){

(单个空格是否被视为有效输入?)

>然后,提交按钮(在表格标签):< input type = button onClick =" CheckFormValues()" style =" height:40px; font- size:18px;" value ="提交新患者 />

(这不是真正的提交按钮(type =" submit"))。 你可以避免按钮的默认操作,如下所示: < input type =" submit" onclick =" return CheckFormValues()" ...> 在验证函数中,返回true / false。您可能还想直接传递 表单元素;这将为你节省getElementById()调用: 函数CheckFormValues(npr){... .. < ; input ... onclick =" return CheckFormValues(this.form)" ...> HTH。 - 康拉德

HI, Just a warning, I''m a javascript neophyte. I''m writing a function to validate the contents of a form on a web page I''m developing. Since I''m a neophyte, this function is quite simple at this time (in fact, I don''t even know if it totally works, I''m still debugging). However, the first problem is that when an error is encountered, I get my alert box, I press ok and then the form is submitted and the new data is entered into the database (and it shouldn''t be). Basically, this is what I''ve got (without all of the form tags): in the <headsection of the page: <script type="text/javascript"> function CheckFormValues() { var nullsPresent = false; var npf = document.getElementById("NewPatientForm"); for(var i=0; i < npf.length; i++) { if(npf.elements[i].nodeType == "textarea") continue; if(npf.elements[i].value == "") { nullsPresent = true; break; } } if(nullsPresent) { alert("No item on this form may be null (except comments) \nPlease correct and resubmit"); return; } npf.submit(); } </script> Then, the submit button (in the form tags): <input type=button onClick="CheckFormValues()" style="height:40px;font- size:18px;" value="Submit New Patient" /> What do I need to have in the function to make this function exit so the user fills in the null strings and then re-submits? As you can see, I tried a "return;" before the call to nfp.submit(), but that didn''t work. It still submitted the data to the DB. Thanks, Andy

解决方案

On 2008-11-17 01:49, Andrew Falanga wrote:

Just a warning, I''m a javascript neophyte.

No problem. I''ve put some recommendations in parentheses.

the first problem is that when an error is encountered, I get my alert box, I press ok and then the form is submitted and the new data is entered into the database (and it shouldn''t be).

...

function CheckFormValues() {

(It''s considered good style in JS to start the names of functions with a lower-case letter unless the function is intended as a constructor)

var nullsPresent = false; var npf = document.getElementById("NewPatientForm"); for(var i=0; i < npf.length; i++) {

(This will evaluate npf.length on every iteration. Since the length is constant, this could be written more efficiently as: for (var i = 0, len = npf.length; i < len; i++) )

if(npf.elements[i].nodeType == "textarea") continue; if(npf.elements[i].value == "") {

(Is a single space considered valid input?)

Then, the submit button (in the form tags): <input type=button onClick="CheckFormValues()" style="height:40px;font- size:18px;" value="Submit New Patient" />

(That''s not really a submit button (type="submit")). You can avoid the default action of the button like this: <input type="submit" onclick="return CheckFormValues()" ...> In the validation function, return true/false. You may also want to pass the form element directly; this will save you the getElementById() call: function CheckFormValues(npr) { ... .. <input ... onclick="return CheckFormValues(this.form)" ...> HTH. - Conrad

"Andrew Falanga" <af******@gmailwrote in message news:5a**********************************@u18g2000 pro.googlegroups...

HI, Just a warning, I''m a javascript neophyte. I''m writing a function to validate the contents of a form on a web page I''m developing. Since I''m a neophyte, this function is quite simple at this time (in fact, I don''t even know if it totally works, I''m still debugging). However, the first problem is that when an error is encountered, I get my alert box, I press ok and then the form is submitted and the new data is entered into the database (and it shouldn''t be). Basically, this is what I''ve got (without all of the form tags): in the <headsection of the page: <script type="text/javascript"> function CheckFormValues() { var nullsPresent = false; var npf = document.getElementById("NewPatientForm"); for(var i=0; i < npf.length; i++) { if(npf.elements[i].nodeType == "textarea") continue; if(npf.elements[i].value == "") { nullsPresent = true; break; } } if(nullsPresent) { alert("No item on this form may be null (except comments) \nPlease correct and resubmit"); return; } npf.submit(); } </script> Then, the submit button (in the form tags): <input type=button onClick="CheckFormValues()" style="height:40px;font- size:18px;" value="Submit New Patient" /> What do I need to have in the function to make this function exit so the user fills in the null strings and then re-submits? As you can see, I tried a "return;" before the call to nfp.submit(), but that didn''t work. It still submitted the data to the DB. Thanks, Andy

Hi Andrew No problem to me. I have learnt how to use JS, but as a programmer I never did come to grips with object oriented languages, so I am still learning I thought about confirm and prompt boxes refer: www.w3schools/js/js_examples_3.asp Window Object But, maybe you could try (not tested) <script type="text/javascript"> var npf = document.getElementById("NewPatientForm"); // npf is global function CheckFormValues() { var nullsPresent = false; for(var i=0; i < npf.length; i++) { if(npf.elements[i].nodeType == "textarea") continue; if(npf.elements[i].value == "") { nullsPresent = true; break; } } return nullsPresent; } if (CheckFormValues()) { // function returned true alert("No item on this form may be null (except comments) \nPlease correct and resubmit"); CheckFormValues(); } else // OK npf.submit(); </script> The function might even work like this function CheckFormValues() { for(var i=0; i < npf.length; i++) { if ( npf.elements[i].nodeType == "textarea" && npf.elements[i].value == "" ) return true; } return false; } -- Trevor Lawrence Canberra Web Site trevorl.mvps

What he said. That is, I gave a solution which should work, with Conrad''s points added -- Trevor Lawrence Canberra Web Site trevorl.mvps "Conrad Lender" <cr******@yahoowrote in message news:eu******************************@supernews.co m...

On 2008-11-17 01:49, Andrew Falanga wrote:

>Just a warning, I''m a javascript neophyte.

No problem. I''ve put some recommendations in parentheses.

>the first problem is that when an error is encountered, I get my alertbox, I press ok and then the form is submitted and the new data isentered into the database (and it shouldn''t be).

..

>function CheckFormValues() {

(It''s considered good style in JS to start the names of functions with a lower-case letter unless the function is intended as a constructor)

> var nullsPresent = false; var npf = document.getElementById("NewPatientForm"); for(var i=0; i < npf.length; i++) {

(This will evaluate npf.length on every iteration. Since the length is constant, this could be written more efficiently as: for (var i = 0, len = npf.length; i < len; i++) )

> if(npf.elements[i].nodeType == "textarea") continue; if(npf.elements[i].value == "") {

(Is a single space considered valid input?)

>Then, the submit button (in the form tags):<input type=button onClick="CheckFormValues()" style="height:40px;font-size:18px;" value="Submit New Patient" />

(That''s not really a submit button (type="submit")). You can avoid the default action of the button like this: <input type="submit" onclick="return CheckFormValues()" ...> In the validation function, return true/false. You may also want to pass the form element directly; this will save you the getElementById() call: function CheckFormValues(npr) { ... .. <input ... onclick="return CheckFormValues(this.form)" ...> HTH. - Conrad

更多推荐

如何退出表单验证函数,以便不提交表单

本文发布于:2023-10-26 21:43:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531433.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   函数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!