处理多个字段的javascript表单

时间:2014-04-02 19:40:32

标签: javascript php html forms error-handling

我有一个表单,从php页面回显,我需要错误处理多个文本字段

“手机”字段的数量不应超过10个 'fname','oname'和'sname'意味着只有字母
并且'日期'意味着只有日期格式。

我如何使用javascript实现这一目标?

echo '
    <html>
        <head>
        <title>Member Registration</title>
        <link rel="stylesheet" type="text/css" href="css.css">
        </head>

<!-- USE JAVASCRIPT FOR ERROR HANDLING -->
    <body>
        <h1>Member Registration</h1>
        <form method="POST" enctype="multipart/form-data" action="register.php" name="regform" onsubmit="return validateForm();return allnumeric()">
            <table>
                <tr>
                    <td><label for="fname">First name: </label> <input class="fields" type="text" name="fname" value="" required></td>
                    <td><label for="sname">Surname: </label> <input class="fields" type="text" name="sname" value="" required></td>
                    <td><label for="oname">Other names: </label> <input class="fields" type="text" name="oname" value="" required></td>
                </tr>
                <tr>
                    <td>Choose Profile Picture:</td><td> <input type="file" name="image" id="file" class="fields"></td>
                </tr>
                <tr>
                    <td><label for="nationalid" >National ID: </label> <input class="fields" type="text" name="nationalid" value="" required></td>
                    <td><label for="dob">Date: </label><input class="fields" type="text" name="dob" required></td>
                </tr>
                <tr>
                    <td><label for="email">Email: </label> <input class="fields" type="text" name="email" value="" required></td>
                </tr>
                <tr>
                    <td><label for="phone">Phone Number: </label> <input  class="fields" type="text" name="phone" value="" required></td>
                </tr>
                <tr>
                    <td><label for="gender">Male</label><input type="radio" value="M" name="gender" required></td>
                    <td><label for="gender">Female</label><input type="radio" value="F" name="gender" required></td>
                </tr>
                <tr>
                    <td><input type="submit" value="Submit" name="submit" onclick="return matchcheck"></td>
                </tr>
            </table>
        </form>
    </body>
    </html>';

这是我到目前为止编写的脚本      

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

function isNumeric(){
var a = document.getElementById('phone').value;
var b = /^\d+$/;

if(a.search(b) == -1)
{
alert(Must be Interger);
return false;
}
}


 </script>

0 个答案:

没有答案