dsp15140275697 2015-04-04 13:36
浏览 38

将焦点设置为用户名输入框(如果已存在而不清除所有字段)

I have a registration form which handle by html5 and javascript validation. The form can't be submitted if there's an invalid input. It give an alert for the invalid input and focused on it. But I do ajax validation to check if username is already exist and it clears all my fields. what I do wrong? How to focus to username field without clearing all my field? Thankyou here is the code:

<script>
function validateForm(){
    var pattEmail = /^\w.+\@+[a-z]+\.[a-z]{2,7}$/;
    if(!pattEmail.test(formSignUp.txt_pelangganEmail.value)) {
        alert("Invalid Email");
        formSignUp.txt_pelangganEmail.focus();
        return false;
    }
var username = formSignUp.txt_pelangganUname.value;
    alert(username);
    $.ajax({
        type: "POST",  
        url: 'ajax/Pelanggan/cekUsername.php',
        async:false,
        dataType: "html",
        data: {
            username:username
        },
        success: function(data) {
            alert(data);
            if (data == 1) {
                alert('username already exist');
                formSignUp.txt_pelangganUname.focus();
                return false;
            }
        }
    });
var txt_pelangganPassword = /^\w{5,}$/;
    if(!txt_pelangganPassword.test(formSignUp.txt_pelangganPassword.value)) {

        //   if(!(form.pword.value.match(/^.\w{5,}$/))){
        alert("Invalid Password");
        formSignUp.txt_pelangganPassword.focus();
        return false;
    }
    if(!(formSignUp.txt_pelangganPassword2.value == formSignUp.txt_pelangganPassword.value)){
        alert("Password tidak sama");
        formSignUp.txt_pelangganPassword2.focus();
        return false;
    }else {
        return true;
    }
}

and this is the form:

<form method="post" action="" name="formSignUp" onSubmit="return validateForm();">
        <table class="tabelSignUp" >
            <tr>
                <td>NAMA DEPAN
                    <br><input type="text" id="txt_pelangganFname" placeholder="Nama Depan" name="txt_pelangganFname" size="40"  pattern="[A-Za-z]+" autofocus="" required=""></td>
            </tr>
            <tr>
                <td>NAMA BELAKANG
                    <br><input type="text" id="txt_pelangganLname" placeholder="Nama Belakang" name="txt_pelangganLname" size="40"  pattern="[A-Za-z]+"></td>
            </tr>
            <tr>
                <td>JENIS KELAMIN
                    <br>
                    <input type="radio" id="pelangganGender" name="pelangganGender" value="P" style="font-size: 12pt;" required="">Perempuan
                    <input type="radio" id="pelangganGender" name="pelangganGender" value="L" style="font-size: 12pt;" required="">Laki-laki
                </td>
            </tr>
            <tr>
                <td>ALAMAT
                    <br><textarea style="word-break: break-word;resize: none;" id="txt_pelangganAlamat" placeholder="Alamat" name="txt_pelangganAlamat" rows="3" cols="35" required=""></textarea></td>
            </tr>
            <tr>
                <td>HANDPHONE
                    <br><input type="text" id="txt_pelangganHp" placeholder="08xxxxxxxxxxx" name="txt_pelangganHp" size="20" maxlength="15"  pattern="[0-9]+" required=""></td>
            </tr>
            <tr>
                <td>TEMPAT LAHIR
                    <br><input type="text" id="txt_pelangganTmpLahir" placeholder="Tempat Lahir" name="txt_pelangganTmpLahir" size="25"  pattern="[A-Za-z ]+" required=""></td>
            </tr>
            <tr>
                <td>TANGGAL LAHIR
                    <br>
                    <select id="txt_tgl" name="txt_tgl" required="">
                        <?php
                        print '<option value="" disabled selected>dd</option>';
                        for ($i = 1; $i <= 31; $i++) {
                            echo "<option value=" . $i . ">" . $i . "</option>";
                        }
                        ?>
                    </select>
                    <select id="txt_bln" name="txt_bln" required="">
                        <?php
                        print '<option value="" disabled selected>mm</option>';
                        for ($i = 1; $i <= 12; $i++) {
                            echo "<option value=" . $i . ">" . $i . "</option>";
                        }
                        ?>
                    </select>
                    <select id="txt_thn" name="txt_thn" required="">
                        <?php
                        print '<option value="" disabled selected>yyyy</option>';
                        for ($i = date("Y") - 60; $i <= date("Y") - 8; $i++) {
                            echo "<option value=" . $i . ">" . $i . "</option>";
                        }
                        ?>
                    </select>
                </td>
            </tr>
            <tr>
                <td>
                    EMAIL
                    <br><input type="email" id="txt_pelangganEmail" placeholder="xxxxx@example.com" name="txt_pelangganEmail" pattern="\w.*+\@+[a-z]+\.[a-z]{2,7}" title="Example: abc@yahoo.com" size="60" required="">
                </td>
            </tr>
            <tr>
                <td>
                    USERNAME
                    <br><input type="text" id="txt_pelangganUname" placeholder="Username" name="txt_pelangganUname" size="60"  pattern="[A-Za-z0-9]+" required="">
                </td>
            </tr>
            <tr>
                <td>
                    PASSWORD
                    <br><input type="password" id="txt_pelangganPassword" placeholder="Password" name="txt_pelangganPassword" required="" maxlength="20">
                </td>
            </tr>
            <tr>
                <td>
                    KONFIRMASI PASSWORD
                    <br><input type="password" id="txt_pelangganPassword2" placeholder="Retype Password" name="txt_pelangganPassword2" required="" maxlength="20">
                </td>
            </tr>
            <tr><td><input type="submit" name="butSignup" value="Sign Up"/></td></tr>
        </table>
    </form>
  • 写回答

1条回答 默认 最新

  • dongyi6845 2015-04-04 18:15
    关注

    Modify your form code as

    <form method="post" action="" name="formSignUp" id="formSignUp" onSubmit="validateForm();return false;">
    

    instead of

    <form method="post" action="" name="formSignUp" onSubmit="return validateForm();">
    

    And manually submit the form from the success part, instead of returning true or false using:

    $("#formSignUp").submit();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有人能看一下我宿舍管理系统的报修功能该怎么改啊?链表那里总是越界
  • ¥15 cs loadimage运行不了,easyx也下了,没有用
  • ¥15 r包runway详细安装教程
  • ¥15 Html中读取Json文件中数据并制作表格
  • ¥15 谁有RH342练习环境
  • ¥15 STM32F407 DMA中断问题
  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。
  • ¥20 关于变压器的具体案例分析