JavaScript表单验证 [英] JavaScript Form Validation

查看:131
本文介绍了JavaScript表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个表单提交,并且在提交表单之前,我想验证这些凭证。



到目前为止,我都有这个

>

为XHTML格式化的HTML

 <?xml version =1.0编码= UTF-8 >?; 
<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Strict // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">

< html xmlns =http://www.w3.org/1999/xhtmlxml:lang =en>
< head>
< title>注册表格< / title>
< meta http-equiv =content-typecontent =text / html; charset = utf-8/>
< meta name =descriptioncontent =这是一个带有验证的表单提交/>
< script type =text / javascriptsrc =validScript.js>< / script>
< / head>

< body>
< h1>登记表< / h1>
< form id =registrationaction =onsubmit =return validation(); method =postenctype =text / plain>
< p>名称:< input type =textname =nameid =name/>< / p>
< p class =error>电子邮件地址:< input type =textname =emailAddressid =emailAddress/>< / p>
< p>< input type =submitname =submitvalue =Submit/>< / p>
< / form>
< / body>

< / html>

JavaScript:

  function validation()
{
var vname = document.registration.name;

if(nameValid(vname))
{
return true;
}
返回false;
}

函数nameValid(vname)
{
var name_len = vname.value.length;
if(name_len == 0)
{
alert(Name is required);
vname.focus();
返回false;
}
返回true;
}

我遇到问题,它不会显示警告用户他/她提示一个空的名字。我试图验证这个名字,在此之后,我会添加更多的函数来添加电子邮件地址和其他字段。请注意,这将稍后用于将表单通过电子邮件发送到域。有人可以帮我弄清楚这个问题吗?



谢谢 你的验证函数是不正确的,应该是:

 函数验证()
{
var vname = document .getElementById( 名称);

if(nameValid(vname))
{
return true;
}
返回false;





$ b

或使用JavaScript库,如 Jquery


I'm trying to create a form submission and before the form is submitted, I want to verify the credentials.

I have both this so far

HTML formatted for XHTML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
        <head>
            <title> Registration Form</title>
            <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
            <meta name="description" content="This is a form submission with validation"/>
            <script type="text/javascript" src="validScript.js"></script>
        </head>

        <body>
            <h1>Registration Form</h1>
            <form id="registration" action="" onsubmit="return validation();" method="post" enctype="text/plain">
                <p>Name: <input type="text" name="name" id="name"/></p>
                <p class="error">Email Address: <input type="text" name="emailAddress" id="emailAddress"/></p>
                <p><input type="submit" name="submit" value="Submit"/></p>
            </form>
        </body>

    </html>

JavaScript:

 function validation()
{  
    var vname = document.registration.name;

    if(nameValid(vname))
    {
        return true;
    }
    return false;
}

function nameValid(vname)
{
    var name_len = vname.value.length;
    if (name_len == 0)
    {
        alert("Name is required");
        vname.focus();
        return false;
    }
return true;
}

I'm having issues where it won't display alert the user that he/she prompted an empty name. I'm trying to valid the name and after this I'll add more functions for email address and other fields to be added. Note that this will later be used to email the form to a domain. Can someone help me figure out this problem?

Thanks

解决方案

your validation function is incorrect, should be:

function validation()
{  
    var vname = document.getElementById("name");

    if(nameValid(vname))
    {
        return true;
    }
    return false;
}

For access form elements values use document.getElementById("ElementID").value or use a javascript library like Jquery

这篇关于JavaScript表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆