Social Security Validation
<!-- Begin
function SSNValidation(ssn) {
var matchArr = ssn.match(/^(\d{3})-?\d{2}
var numDashes = ssn.split('-').length - 1;
if (matchArr == null || numDashes == 1) {
alert('Invalid SSN. Must be 9 digits or in the form NNN-NN-NNNN.');
msg = "does not appear to be valid";
}
else
if (parseInt(matchArr[1],10)=
alert("Invalid SSN: SSN's can't start with 000.");
msg = "does not appear to be valid";
}
else {
msg = "appears to be valid";
alert(ssn + "\r\n\r\n" + msg + " Social Security Number.");
}
}
// End -->
</script>
</HEAD>
<!-- STEP TWO: Copy this code into the BODY of your HTML document -->
<BODY>
<form>
<tt>
SS #: <input type=text name=ssn size=11 maxlength=11> (use dashes!)
</tt>
<p>
<input type=button value="Validate Number" onClick="SSNValidation(thi
</form>
<p><center>
<font face="arial, helvetica" size"-2">Free JavaScripts provided<br>
by <a href="http://javascriptsou
</center><p>
<!-- Script Size: 1.54 KB
Phone Number validation
<script language = "Javascript">
/**
* DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com
*/
// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;
function isInteger(s)
{ var i;
for (i = 0; i < s.length; i++)
{
// Check that current character is number.
var c = s.charAt(i);
if (((c < "0") || (c > "9"))) return false;
}
// All characters are numbers.
return true;
}
function stripCharsInBag(s, bag)
{ var i;
var returnString = "";
// Search through string's characters one by one.
// If character is not in bag, append to returnString.
for (i = 0; i < s.length; i++)
{
// Check that current character isn't whitespace.
var c = s.charAt(i);
if (bag.indexOf(c) == -1) returnString += c;
}
return returnString;
}
function checkInternationalPhone(st
s=stripCharsInBag(strPhone
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}
function ValidateForm(){
var Phone=document.frmSample.t
if ((Phone.value==null)||(Pho
alert("Please Enter your Phone Number")
Phone.focus()
return false
}
if (checkInternationalPhone(P
alert("Please Enter a Valid Phone Number")
Phone.value=""
Phone.focus()
return false
}
return true
}
</script>
<form name="frmSample" method="post" action="#" onSubmit="return ValidateForm()">
<p>Enter a phone number :
<input type="text" name="txtPhone">
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
Credit card validation
<script language="JavaScript">
// Form Field Validation Functions:
//
// isValidExpDate(formField,f
// -- checks for date in the format MM/YY or MM/YYYY against the current date
// isValidCreditCardNumber(fo
// -- checks for valid credit card format using the Luhn check and known digits about various cards
//
function validRequired(formField,fi
{
var result = true;
if (formField.value == "")
{
alert('Please enter a value for the "' + fieldLabel +'" field.');
formField.focus();
result = false;
}
return result;
}
function allDigits(str)
{
return inValidCharSet(str,"012345
}
function inValidCharSet(str,charset
{
var result = true;
for (var i=0;i<str.length;i++)
if (charset.indexOf(str.subst
{
result = false;
break;
}
return result;
}
function isValidExpDate(formField,f
{
var result = true;
var formValue = formField.value;
if (required && !validRequired(formField,f
result = false;
if (result && (formField.value.length>0)
{
var elems = formValue.split("/");
result = (elems.length == 2); // should be two components
var expired = false;
if (result)
{
var month = parseInt(elems[0],10);
var year = parseInt(elems[1],10);
if (elems[1].length == 2)
year += 2000;
var now = new Date();
var nowMonth = now.getMonth() + 1;
var nowYear = now.getFullYear();
expired = (nowYear > year) || ((nowYear == year ) && (nowMonth > month));
result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
allDigits(elems[1]) && ((elems[1].length == 2) || (elems[1].length == 4));
}
if (!result)
{
alert('Please enter a date in the format MM/YY for the "' + fieldLabel +'" field.');
formField.focus();
}
else if (expired)
{
result = false;
alert('The date for "' + fieldLabel +'" has expired.');
formField.focus();
}
}
return result;
}
function isValidCreditCardNumber(fo
{
var result = true;
var ccNum = formField.value;
if (required && !validRequired(formField,f
result = false;
if (result && (formField.value.length>0)
{
if (!allDigits(ccNum))
{
alert('Please enter only numbers (no dashes or spaces) for the "' + fieldLabel +'" field.');
formField.focus();
result = false;
}
if (result)
{
if (!LuhnCheck(ccNum) || !validateCCNum(ccType,ccNu
{
alert('Please enter a valid card number for the "' + fieldLabel +'" field.');
formField.focus();
result = false;
}
}
}
return result;
}
function LuhnCheck(str)
{
var result = true;
var sum = 0;
var mul = 1;
var strLen = str.length;
for (i = 0; i < strLen; i++)
{
var digit = str.substring(strLen-i-1,s
var tproduct = parseInt(digit ,10)*mul;
if (tproduct >= 10)
sum += (tproduct % 10) + 1;
else
sum += tproduct;
if (mul == 1)
mul++;
else
mul--;
}
if ((sum % 10) != 0)
result = false;
return result;
}
function GetRadioValue(rArray)
{
for (var i=0;i<rArray.length;i++)
{
if (rArray[i].checked)
return rArray[i].value;
}
return null;
}
function validateCCNum(cardType,car
{
var result = false;
cardType = cardType.toUpperCase();
var cardLen = cardNum.length;
var firstdig = cardNum.substring(0,1);
var seconddig = cardNum.substring(1,2);
var first4digs = cardNum.substring(0,4);
switch (cardType)
{
case "VISA":
result = ((cardLen == 16) || (cardLen == 13)) && (firstdig == "4");
break;
case "AMEX":
var validNums = "47";
result = (cardLen == 15) && (firstdig == "3") && (validNums.indexOf(secondd
break;
case "MASTERCARD":
var validNums = "12345";
result = (cardLen == 16) && (firstdig == "5") && (validNums.indexOf(secondd
break;
case "DISCOVER":
result = (cardLen == 16) && (first4digs == "6011");
break;
case "DINERS":
var validNums = "068";
result = (cardLen == 14) && (firstdig == "3") && (validNums.indexOf(secondd
break;
}
return result;
}
function validCCForm(ccTypeField,cc
{
var result = isValidCreditCardNumber(cc
isValidExpDate(ccExpField,
return result;
}
</script>
<form name="testform">
<p>Card Type:
<select name="ccType">
<option value="VISA" selected>Visa
<option value="MASTERCARD">MasterC
<option value="AMEX">American Express
<option value="DISCOVER">Discover
<option value="DINERS">Diners Club</option>
</select><br>
Credit Card Number: <input name="ccNum" size="19" maxlength="19" ><br>
Expiration Date (MM/YY): <input name="ccExp" size="7" ><br>
<input type="button" name="validateCC" value="Validate Card" onClick="alert('Credit card valid = ' + validCCForm(this.form.ccTy
</p>
</form>
Main Topics
Browse All Topics





by: BustaroomsPosted on 2004-07-14 at 13:03:57ID: 11553160
several ways, one being isNaN()
The function isNaN() returns True if the argument is NOT a number - NaN = Not a number, get it
im sure someone will give you a nice regex to do the trick as well