...

View Full Version : RegExpr Conundrum



MindCrafter
06-30-2004, 12:45 AM
Okay its late and I've run out of Jolt Cola...

I have a stock javascript scriptlet (shown below) that I use to validate both International and US phone number form fields.
Below are the two validation RegEpressions that I have used in the past.

RegExpr
International
/^x\s*[0-9]{5}$)|(^(\([1-9][0-9]{2}\)\s)?[1-9][0-9]{2}-[0-9]{4}(\sx\s*[0-9]{5})?$/
and
US (###) ###-####
/^((\(?[1-9][0-9][0-9]\)?)?\s?[1-9][0-9][0-9]\s?\d{4})?$/


Problem is, neither Regexp syntax are working in a ColdFusionMX webpage.

Validation Code
function chkPhone(sFieldName,sFieldValue){
var regExpr = /^((\(?[1-9][0-9][0-9]\)?)?\s?[1-9][0-9][0-9]\s?\d{4})?$/;
if (!regExpr.test(sFieldValue)){
alert('Invalid phone number format detected.\nPlease enter a valid phone number. e.g. \(###\) ###\-#### (123) 456\-7890');
document.frmCreateNewUser.elements[sFieldName].className='DataEntryFieldsErr';
document.frmCreateNewUser.elements[sFieldName].value='\(###\) ###\-####';
document.frmCreateNewUser.elements[sFieldName].focus;
}else{
document.frmCreateNewUser.elements[sFieldName].className='DataEntryFieldsEnabled';
}
}


Form Field Code:
<input type="text"
id="txtWorkPhone"
name="txtWorkPhone"
class="DataEntryFieldsEnabled"
tabindex="6"
onchange="if(this.value!='\(###\) ###\-####')
chkPhone(this.name,this.value);"
onFocus="this.className='FieldHighlight';ClearHighlight(this.name);"
value="(###) ###-####"
maxlength="15" />


A test copy of the form can be viewed at
http://www.mindcrafter.com/testform1.cfm (http://http://www.mindcrafter.com/testform1.cfm)
Any Suggestions?

MindCrafter
DaleM@MindCrafter.com

MindCrafter
06-30-2004, 03:35 AM
Well, I went upstairs had a PBJ and a large glass of milk...

Wnen I typed the RegExpr, I goofed.


Original Expressions
/^((\(?[1-9][0-9][0-9]\)?)?\s?[1-9][0-9][0-9]\s?\d{4})?$/


Fixed Expression
/^((\([1-9][0-9][0-9]\))\s[1-9][0-9][0-9]\-\d{4})$/

Since I want to force the area code I just removed the question marks. Also I had a space escape character instead of a dash separating the first 3 and last 4 numeral in the phone number.

Guess I better update my DW snippets!

MindCrafter
www.mindcrafter.com



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum