Phone Number and Credit Card Number Validation

Dr Octogonapus

Jōnin Strategist 🧠
Regular
Joined
Nov 10, 2009
Messages
1,522
Reaction score
29
Hello i'm doing javascript validation on my form and I don't know how to do phone number validation so it only has numbers and single spaces allowed and also credit card validation which I want it to display between 13 and 18 numbers and single spaces.

Javascript:

Code:
<script type="text/javascript">
 
function validateForm()
 
{
 
    var x=document.forms["checkout_details"]["firstname"].value;
 
    if (x==null || x=="")
 
    {
 
        alert("Please fill out your First Name");
 
        return false;
 
    }
 
   
 
    var x=document.forms["checkout_details"]["lastname"].value;
 
    if (x==null || x=="")
 
    {
 
        alert("Please fill out your Last Name");
 
        return false;
 
    }
 
   
 
    var x=document.forms["checkout_details"]["address"].value;
 
    if (x==null || x=="")
 
    {
 
        alert("Please fill out your Address");
 
        return false;
 
    }
 
   
 
    var x=document.forms["checkout_details"]["email"].value;
 
    var atpos=x.indexOf("@");
 
    var dotpos=x.lastIndexOf(".");
 
    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
 
    {
 
        alert("Your Email Address is not valid. Please fill out a valid Email Address");
 
        return false;
 
    }
 
   
 
   
 
}
 
</script>

HTML/PHP:

Code:
echo'<form name="checkout_details" action="confirm.php" onsubmit="return validateForm()" method="post">';
 
echo'<font color="red">*</font> <b>First Name:</b> <input type="text" name="firstname"><br /><br />';
 
echo'<font color="red">*</font> <b>Last Name:</b> <input type="text" name="lastname"><br /><br />';
 
echo'<font color="red">*</font> <b>Address:</b> <textarea name="address" rows="4" cols="20"></textarea><br /><br />';
 
echo'<font color="red">*</font> <b>Email Address:</b> <input type="text" name="email"><br /><br />';
 
echo'<font color="red">*</font> <b>Phone Number:</b> <input type="text" name="phone"><br /><br />';
 
echo'<font color="red">*</font> <b>Delivery Method:</b><br />';
 
echo'<input type="radio" name="radio" id="regularpost" checked="true">Regular Post<br />';
 
echo'<input type="radio" name="radio" id="courier">Courier<br />';
 
echo'<input type="radio" name="radio" id="expresscourier">Express Courier<br /><br />';
 
echo'<font color="red">*</font> <b>Please sign me up for the newsletter:</b> <input type="checkbox" name="checkbox"><br /><br />';
 
echo'<input type="submit" value="Purchase">';
 
echo'</form>';

Can anyone please help me out with this? Thankyou.
 

Dr Octogonapus

Jōnin Strategist 🧠
Regular
Joined
Nov 10, 2009
Messages
1,522
Reaction score
29
How to do the phone number and credit card validation?

I'm not sure how to do it and am in need of the javascript code for those 2 things to finish off my form.
 

Wabbit

Banned
Legendary
Joined
Nov 15, 2011
Messages
11,335
Reaction score
797
dude these questions are supposed to be asked on
 
Top