/    Sign up×
Community /Pin to ProfileBookmark

Form validation with RegExp

Hi all,

I hope that everyone is well. If anyone has time, please can they help me with the following.

I’m attempting to validate a few fields using regexp but for some reason, only one field is being successfully validated. I’ve written four functions, three to perform the validations and one to bring them all together, but after looking at it for the last few minutes, I’m at wits end. I’d be very grateful for any help.

This is the validating code (in an external js file): –

[CODE]//Script Name: Form Validation
//Version: 1.0
//Created by: Steven Andrews
//Date : 1st April 2007
/////////////////////////////////
// Function: TelNoCheck
// Purpose: To confirm that a telephone number starts with 4 or 5 digits, followed by a space, and then 6 or 76 other numbers.
// Input: value : a String to test
// Output: TRUE if telephone number meets criteria
// FALSE if not
function TelNoCheck(value)

{

var re_telnumber = /^d{4,5}sd{6,7}$/

var result;

if (re_telnumber.test(value)) {

return true;

} else {

return false;

}

} // TelNoCheck()

// Function: LaNameCheck
// Purpose: This is to confirm that the client submits a surname of at least two letters.
// Input: value : a String to test
// Output: TRUE if name meets criteria
// FALSE if not
function LaNameCheck(value)

{

var re_lastname = /^w[a-z]+$/

var result;

if (re_lastname.test(value)) {

return true;

} else {

return false;

}

} // LaNameCheck()

// Function: EmailCheck
// Purpose: This
// Input: value : a String to test for more then one character either side of the @ symbol before searching for a domain name. The domain name will take ox.ac.uk as well as .edu but will not take domain longer then four letters
// Output: TRUE if email meets criteria
// FALSE if not

function EmailCheck(value)

{

var re_email = /^w+([.-]?w)*@w+([.-]?w+)*(.w{3})+$/

var result;

if (reg.test(value)) {

return true;

} else {

return false;

}

} // EmailCheck()

// Function: validForm
// Purpose: Overall form checking function
// Input: passForm : Form object to check
// Output: valid = TRUE if all tests pass
// valid = FALSE if at least on test fails

function validForm (passForm) {

var valid = true;

// check the name format

if (! LaNameCheck(passForm.billFamilyName.value)) {

alert(“You must enter a valid name!”);

valid = false;

}

// check the e-mail address format

if (! EmailCheck(passForm.billEmailAddress.value)) {

alert(“You must enter a valid email!”);

valid = false;

}

// check the telephone number format

if (! TelNoCheck(passForm.billPhoneNumber.value)) {

alert(“You must enter a valid number!”);

valid = false;

}

return valid;

} // validForm()[/CODE]

And my form code is as follows: –

[CODE]<form method=”post” action=”http://telemat.open.ac.uk/tt281/testform/decode_form.cfm” onsubmit=”return validForm(this);”>
<table cellpadding=”5″>
<tr>
<td><h3>Shipping Information</h3></td>
<td>&nbsp;</td>
</tr>
<tr>
<td><label for=”shipFirstName”>First Name</label></td>
<td><input type=”text” name=”shipFirstName” id=”shipFirstName” maxlength=”30″></td>
</tr>
<tr>
<td><label for=”shipFamilyName”>Family Name</label></td>
<td><input type=”text” name=”shipFamilyName” id=”shipFamilyName” maxlength=”30″></td>
</tr>
<tr>
<td><label for=”shipPhoneNumber”>Contact Telephone Number</label></td>
<td><input type=”text” name=”shipPhoneNumber” id=”shipPhoneNumber” maxlength=”30″></td>
</tr>
<tr>
<td><label for=”shipHouseNumber”>House Number</label></td>
<td><input type=”text” name=”shipHouseNumber” id=”shipHouseNumber” maxlength=”30″></td>
</tr>
<tr>
<td><label for=”shipStreetName”>Street Name</label></td>
<td><input type=”text” name=”shipStreetName” id=”shipStreetName” maxlength=”30″></td>
</tr>
<tr>
<td><label for=”shipCity”>City</label></td>
<td><input type=”text” name=”shipCity” id=”shipCity” maxlength=”30″></td>
</tr>
<tr>
<td><label for=”shipPostalCode”>Postal Code</label></td>
<td><input type=”text” name=”shipPostalCode” id=”shipPostalCode” maxlength=”30″></td>
</tr>
<tr>
<td><label for=”shipPostalCode”>Type of Pan </label></td>
<td><input type=”text” name=”shipPanType” id=”shipPanType” maxlength=”30″></td>
</tr>
<SCRIPT>var obj = actb(document.getElementById(‘shipPanType’),customarray);
//setTimeout(function(){obj.actb_keywords = custom2;},10000);</SCRIPT>
<tr>
<td><h3>Billing Information</h3></td>
<td>&nbsp;</td>
</tr>
<tr>
<td><label for=”billFirstName”>First Name</label></td>
<td><input type=”text” name=”billFirstName” id=”billFirstName” maxlength=”30″></td>
</tr>
<tr>
<td><label for=”billFamilyName”>Family Name</label></td>
<td><input type=”text” name=”billFamilyName” id=”billFamilyName” maxlength=”30″></td>
</tr>
<tr>
<td><label for=”billEmailAddress”>Email Address</label></td>
<td><input type=”text” name=”billEmailAddress” id=”billEmailAddress” maxlength=”30″></td>
</tr>
<tr>
<td><label for=”billPhoneNumber”>Contact Telephone Number</label></td>
<td><input type=”text” name=”billPhoneNumber” id=”billPhoneNumber” maxlength=”30″></td>
</tr>
<tr>
<td><label for=”billCardNumber”>Credit Card Number</label></td>
<td><input type=”text” name=”billCardNumber” id=”billCardNumber” maxlength=”30″></td>
</tr>
<tr>
<td><label for=”billCardType”>Credit Card Type</label></td>
<td><input type=”text” name=”billCardType” id=”billCardType” maxlength=”30″>Visa/MasterCard</td>
</tr>
<tr>
<td><label for=”instructions”>Special Instructions</label></td>
<td>
<textarea name=”instructions” id=”instructions” rows=”8″ cols=”30″>Enter your requirements here or comments.</textarea></td>
</tr>
<tr>
<td><input type=”submit” name=”Submit” id=”Submit” value=”Submit”></td>
<td>&nbsp;</td>
</tr>
</table>
</form>[/CODE]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@disgracianApr 01.2007 — For starters, the regex is wrong:
var re_telnumber = /^d{4,5}sd{6,7}$/
The second 'd' needs to be backslashed otherwise it's treated as a literal 'd'.
/^w[a-z]+$/
This will only match an unbroken string of lowercase letters, and no spaces. What if the person's name is "da Vinci" or something? Perhaps a regex of [b]/^[a-zA-Z]+s?[a-zA-Z]+$/[/b] would be better.

Cheers,

D.
Copy linkTweet thisAlerts:
@skriptorApr 02.2007 — Hi,

instead of

if (reg.test(value)) {

use

if (re_email.test(value)) {

Good Luck, skriptor
Copy linkTweet thisAlerts:
@SeifauthorApr 02.2007 — That worked great guys, thanks very much! Please can someone mark this as solved?
×

Success!

Help @Seif spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...