0

我已经开始编写用户注册验证。这个表单是为了让用户可以在网站上注册,但是我在如何正确地为每个字段编写验证方面遇到了问题。

至今:

<div id="content">
        <div class="ic"></div>
        <div class="inner">
            <div class="container_12">
                <div class="wrapper">
                    <div class="grid_12">
                    </div>
                </div>
                <div class="wrapper">
                    <div class="grid_12">
                        <h2 class="h-pad1">Open an Account with Us</h2>
                        <form action="" id="validate" class="form" method="POST">
            <fieldset>
                <div class="formRow">
                    <label for="login">Username:</label>
                    <div class="loginInput"><input type="text" name="username" class="validate[required]" id="username" maxlength="15"/></div>
                    <div class="clear"></div>
                </div>

                <div class="formRow">
                    <label for="pass">Password:</label>
                    <div class="loginInput"><input type="password" name="password" class="validate[required]" id="pass" /></div>
                    <div class="clear"></div>
                </div>

                <div class="formRow">
                    <label for="pass">Repeat Password:</label>
                    <div class="loginInput"><input type="password" name="rpassword" class="validate[required]" id="rpass" /></div>
                    <div class="clear"></div>
                </div>

                <div class="formRow">
                    <label for="pass">Email:</label>
                    <div class="loginInput"><input type="text" name="email" class="validate[required]" id="email" /></div>
                    <div class="clear"></div>
                </div>

添加的正确代码是什么?谢谢你。

4

2 回答 2

1

如果您要使用常规 javascript,您最重要的任务是学习如何使用document.getElementById() 。使用该功能,您可以获取不同字段中的值并检查其中存储的内容。

我将有一个高级函数validateRegistrationForm()调用用于验证每个字段的函数。例如

function validateRegistrationForm(){
     if(!validateEmailField()) // Invalid email format
          return;
     if(!validatePasswordField()) // Invalid password length
          return;
     if(validateUserNameField()) // invalide characters in email
          return;
     .
     .
    etc.
}

function validateEmailField(){
    var x=document.getElementById("email").value;
    var atpos=x.indexOf("@");
    var dotpos=x.lastIndexOf(".");
    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
    {
        errorPopup("Not a valid e-mail address");
        return false;
    }

    if(email==x) //If the old email is equal to the current then perform no check for uniqueness. On the server side the email portion will be ignored.
        return true;

    return isEmailUnique(); // return true if the email is unique
}
于 2013-05-01T05:06:14.137 回答
0

你可以有类似的东西:

function validate()
{

var uName = document.getElementById('username');

if(uName == '' or uName=' ')
{

 // do something

}

repeat for other elements.

.
.
.
.

// in the end, you can return true; if all validation condition meet or else return false;

}
于 2013-05-01T04:30:12.150 回答