简体   繁体   English

提交前进行CFForm验证和onclick

[英]CFForm validation and onclick before submit

I have been trying to validate a few fields before submitting a form. 在提交表单之前,我一直在尝试验证一些字段。 Once the fields are validated by CF I want to pop up a sign in form if the user is not signed in. However, the problem is that the sign in form is pop up without validation. 一旦通过CF验证了字段,如果用户未登录,我想弹出一个登录表单。但是,问题是没有验证就弹出了登录表单。

inquiry.cfm query.cfm

<cfif not isdefined("form.p1")>
    <cfform name="inquire" action="inquiry.cfm" method="post">
    <input type="hidden" name="p1" value="1">
    <cfinput type="text" name="email" size="50" maxlength="50" required="yes" autofocus="on" validate="email">
    <cfinput type="text" name="subject" size="50" maxlength="50" required="yes" validate="noblanks">
    <cftextarea name="message" cols="45" rows="8" wrap="soft" required="yes" validate="noblanks"></cftextarea>
    <cfif signedin>
        <input type="submit" value=" Send "> 
    <cfelse>
        <input type="submit" value=" Send " onclick="signin(); return false">
    </cfif>
<cfelse>
    do stuff....
</cfif>

The "signin" function will pop up the sign in form. “登录”功能将弹出登录表单。

What I have tried so far besides the above: 到目前为止,我还尝试了以下方法:

  1. onSubmit instead of onClick. onSubmit而不是onClick。 But I don't want to submit the form until the user is signed in. 但我不想在用户登录之前提交表单。
  2. putting the onClick in the CFFORM tag but CF validation and sign in screen will pop up at the same time. 将onClick放入CFFORM标记中,但CF验证和登录屏幕将同时弹出。 After dismiss the CF fields validation screen but only complete the sign in screen will submit the blank form ie no validation. 在关闭CF字段验证屏幕但仅完成登录屏幕后,将提交空白表格,即不进行验证。
  3. using CFINPUT tag instead of INPUT. 使用CFINPUT标记而不是INPUT。 Made no difference but also messed up the button text. 没有影响,但也弄乱了按钮文本。

Any help is appreciated. 任何帮助表示赞赏。 Thanks in advance. 提前致谢。

@Jack, This is just an example of form fields validation using ajax. @Jack,这只是使用Ajax进行表单字段验证的示例。 In order to use it you may need to make some changes to it. 为了使用它,您可能需要对其进行一些更改。 Moreover, I preferred using html forms in this simple example. 而且,我更喜欢在这个简单的示例中使用html表单。 Let say we have a validatoin.cfc with a method name 'validation' which takes an arg. 假设我们有一个validatoin.cfc,其方法名称为“ validation”,该参数带有一个arg。

  <cffunction name="validation" access="remote" returntype="any" returnformat="JSON" output="false">
    <cfargument name="args" type="any" default="">
   <cfset var retval ='{"return":"false"}'>
    <cfif len(trim(arguments.args))>
        <cfset retval =  '{"return":"true"}'>
    </cfif>
    <cfreturn retval>
</cffunction>

And here is code for cfm file. 这是cfm文件的代码。

    <div  id="target">
  <form name="inquire" action="TestOne.cfm" method="post">
    <input type="hidden" name="p1" value="1">
    <input id="email" type="text" name="email" size="50" maxlength="50" required="yes" autofocus="on" validate="email" ><br><br>
    <input type="text" name="subject" size="50" maxlength="50" required="yes" validate="noblanks" ><br><br>
    <textarea name="message" cols="45" rows="8" wrap="soft" required="yes" validate="noblanks"  ></textarea>
  </form>
  </div>

<script>
    jQuery("#target").on( "click", function(){
        var d = $('#email').val();
        $.ajax({
            type: 'POST',
            url: 'validation.cfc?method=validation',
            dataType: "json",
            data: { args: d },
            success: function(data) {
                 if(data.return != 'true'){
                     alert("Please enter an valid email address");                 }
            }
        });
    }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM