javascript手机验证

时间:2016-04-19 09:46:37

标签: javascript php

<?php
//include the DB connection
include("includes/config.php");
//include the header
include("includes/header.php");
?>
<?php
//checks if the form was submitted
if(isset($_POST['submit']))
{
    //get form values when submitted
    $name=$_POST['name'];
    $email=$_POST['email'];
    $phone=$_POST['phone'];
    $password=$_POST['password'];
    $subject=$_POST['subject'];
    $target_dir = "uploads/";
    $target_file = $target_dir . time() . basename($_FILES["file"]["name"]);

    if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) 
    {

        mysql_query("INSERT INTO `login`(`name`,`email`,`phone`,`password`,`file`,`subject`)VALUES('$name','$email','$phone','$password',
            '$target_file','$subject')");

        echo "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.";
    } 
    else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

<div class="form">
<form action="" method="post" name="registration" enctype="multipart/form-data" onSubmit="return formValidation();">
    <table width="500" border="1" cellspacing="0" cellpadding="0" class="table">
      <tr>
        <td>Name</td>
        <td><input name="name" type="text" class="input" placeholder="Please Enter Your Name" value="<?php //echo $userName ;?>" /></td>
      </tr>
      <tr>
        <td>Email</td>
        <td><input name="email" type="email" class="input" placeholder="Please Enter Your Email ID "value=" <?php //echo $userEmail ;?>"  /></td>
      </tr>
      <tr>
      <td>Phone</td>
        <td><input name="phone" type="text"  class="input" placeholder="Please Enter Your Phone  Number" value="<?php //echo $userPhone;?>" /></td>
      </tr>
      <tr>
        <td>Password</td>
        <td><input name="password" type="password" class="input" value="" /></td>
      </tr>
      <tr>
        <td>Upload file</td>
        <td><input name="file" type="file" class="input" value="" </td>



      </tr>

      <tr>
      <td>Subject</td>

        <td><textarea name="subject" cols="" rows="" class="input" placeholder="Please Enter Your Query "/> <?php //echo $userSubject;?></textarea></td>
      </tr>
        <tr>
      <td></td>
      <td><input name="submit" type="submit" value="Submit" /></td>
      </tr>


        </table>
</form>     
</div>

<?php
//include the footer
include("includes/footer.php");
?>

<script type="text/javascript">
function formValidation()  
{   
    var uname = document.registration.name; 
    var uphone= document.registration.phone;
    if(allLetter(uname))  
    {
        if(phonenumber(uphone))  
        {
        }
    }
    return false;
}

function allLetter(uname)  
{   

    var letters = /^[A-Za-z]+$/;  
    if(uname.value.match(letters))  
    {  

        return true;  
    }  
    else  
    {  
        alert('Username must have alphabet characters only');  
        uname.focus();  
        return false;  
    }  
} 

function phonenumber(inputtxt)  
{  
    var phoneno = /^\d{10}$/;  
    if((inputtxt.value.match(phoneno))  
    {  
      return true;  
    }  
   else
    {  
        alert("message");  
        return false;  
    }  
}  
</script>

对于用户名,验证代码有效,但是对于电话验证,它无法正常工作,我希望手机验证空白字段检查和格式检查和电子邮件检查。帮助我,因为我是javascript的新手

2 个答案:

答案 0 :(得分:0)

试试这段代码。

function formValidation()
    {
    var phone=document.forms["registration"]["phone"].value;
    var phone_num = /^\d{10}$/;  
        if (phone=="")
        {
            alert("enter your phone number");
            return false;
        }
        else if(phone) 
        {
            if(!phone_num.test(phone)) 
        {  
         alert("Not a valid Phone Number");  
         return false;
        }
        }
        var email = document.forms["registration"]["email"].value;
        if (email=="")
            {
                alert("enter your email id");
                return false;
            }
        var atpos = email.indexOf("@");
        var dotpos = email.lastIndexOf(".");
        if (atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length)
        {
            alert("Not a valid e-mail address");
            return false;
        }
    }

答案 1 :(得分:0)

您的手机功能中typo额外(

尝试:

if(inputtxt.value.match(phoneno))

而不是if((inputtxt.value.match(phoneno))

PS:提示在使用JS

时查看并使用控制台工具