Learning PHP and MySQL
Tuesday, September 26, 2006
Example 15-2 The file source.js contains functions to check the various fields
// verify username - 6-10 chars, uc, lc, and underscore only.
function verify_username (strng) {
var error = "";
if (strng == "") {
error = "You didn't enter a username.\n";
}
var illegalChars = /\W/; // allow letters, numbers, and underscores
if ((strng.length < 6) || (strng.length > 10)) {
error = "The username is the wrong length. It must be 6-10 characters.\n";
}
else if (illegalChars.test(strng)) {
error = "The username contains illegal characters.\n";
}
return error;
}
// verify password - between 6-8 chars, uppercase, lowercase, and numeral
function verify_password (strng) {
var error = "";
if (strng == "") {
error = "You didn't enter a password.\n";
}
var illegalChars = /[\W_]/; // allow only letters and numbers
if ((strng.length < 6) || (strng.length > 8)) {
error = "The password is the wrong length. It must be 6-8 characters.\n";
}
else if (illegalChars.test(strng)) {
error = "The password contains illegal characters.\n";
}
else if (!((strng.search(/(a-z)+/)) && (strng.search(/(A-Z)+/)) &&
(strng.search(/(0-9)+/)))) {
error = "The password must contain at least one uppercase letter, one
lowercase letter, and one numeral.\n";
}
return error;
}
// verify email
function verify_email (strng) {
var error="";
if (strng == "") {
error = "You didn't enter an email address.\n";
}
var emailFilter=/^.+@.+\..{2,3}$/;
if (!(emailFilter.test(strng))) {
error = "Please enter a valid email address.\n";
}
else {
//test email for illegal characters
var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
if (strng.match(illegalChars)) {
error = "The email address contains illegal characters.\n";
}
}
return error;
}
// verify phone number - strip out delimiters and verify for 10 digits
function verify_phone (strng) {
var error = "";
if (strng == "") {
error = "You didn't enter a phone number.\n";
}
//strip out acceptable non-numeric characters
var stripped = strng.replace(/[\(\)\.\-\ ]/g, '');
if (isNaN(parseInt(stripped))) {
error = "The phone number contains illegal characters.";
}
if (!(stripped.length == 10)) {
error = "The phone number is the wrong length. Make sure you included an area
code.\n";
}
return error;
}
Posted by krautgrrl on 09/26 at 02:58 PM
Chapter 15 Code • (0) Comments • (91) Trackbacks • Permalink
Chapter 15 Code • (0) Comments • (91) Trackbacks • Permalink
Statistics
This page has been viewed 375209 times
Page rendered in 0.2203 seconds
Total Entries: 224
Total Comments: 16
Total Trackbacks: 307338
Most Recent Entry: 09/27/2006 12:39 pm
Most Recent Comment on: 10/26/2007 10:00 am
Total Members: 2
Total Logged in members: 0
Total guests: 9
Total anonymous users: 0
Most Recent Visitor on: 02/10/2012 09:42 am
The most visitors ever was 1103 on 11/20/2007 12:50 pm
