php - javascript phone validataion -
<?php //include db connection include("includes/config.php"); //include header include("includes/header.php"); ?> <?php //checks if form 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 `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 error uploading 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 name" value="<?php //echo $username ;?>" /></td> </tr> <tr> <td>email</td> <td><input name="email" type="email" class="input" placeholder="please enter email id "value=" <?php //echo $useremail ;?>" /></td> </tr> <tr> <td>phone</td> <td><input name="phone" type="text" class="input" placeholder="please enter 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 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 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>
for user name code validation works phone validation it's not working,i want phone validation empty field check , format check , email check.help me out i'm new javascript
try code.
function formvalidation() { var phone=document.forms["registration"]["phone"].value; var phone_num = /^\d{10}$/; if (phone=="") { alert("enter phone number"); return false; } else if(phone) { if(!phone_num.test(phone)) { alert("not valid phone number"); return false; } } var email = document.forms["registration"]["email"].value; if (email=="") { alert("enter email id"); return false; } var atpos = email.indexof("@"); var dotpos = email.lastindexof("."); if (atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length) { alert("not valid e-mail address"); return false; } }
Comments
Post a Comment