Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

creating custom form verification 1

Status
Not open for further replies.

leadman

Programmer
Joined
Jun 11, 2001
Messages
177
Location
US
Im using form verification on cfform elements and would like to verify that an email address is put into the the email field. I see there is a choice in cf studio to check for phone number (among others) but none for email. Is there a way to make it myself (perhaps to check for a "@")?
 
copy the following script into the head of your template:

function validEmail(TheForm, TheField, TheValue) {
invalidChars = &quot; /:,;<>?&quot;
if (TheValue == &quot;&quot;) {
document.newMemberForm.submitNew.value=&quot;Submit Info&quot;
return false
}

for (i=0; i<invalidChars.length; i++) {
badChar = invalidChars.charAt(i)
if (TheValue.indexOf(badChar,0) > -1) {
document.newMemberForm.submitNew.value=&quot;Submit Info&quot;
return false
}
}
atPos = TheValue.indexOf(&quot;@&quot;,1)
if (atPos == -1) {
document.newMemberForm.submitNew.value=&quot;Submit Info&quot;
return false
}
if (TheValue.indexOf(&quot;@&quot;,atPos+1) > -1) {
document.newMemberForm.submitNew.value=&quot;Submit Info&quot;
return false
}
periodPos = TheValue.indexOf(&quot;.&quot;,atPos)
if (periodPos == -1) {
document.newMemberForm.submitNew.value=&quot;Submit Info&quot;
return false
}
if (periodPos == atPos+1) {
document.newMemberForm.submitNew.value=&quot;Submit Info&quot;
return false
}
if (periodPos+3 > TheValue.length) {
document.newMemberForm.submitNew.value=&quot;Submit Info&quot;
return false
}
return true
}


in the form use this text field as email field:

<cfinput type=&quot;text&quot; name=&quot;email&quot; size=&quot;30&quot; maxlength=&quot;30&quot; onvalidate=&quot;validEmail&quot; value=&quot;&quot; onfocus=&quot;this.select()&quot;>


replace the submit button with this one:

<input type=&quot;submit&quot; name=&quot;submitNew&quot; value=&quot;Submit Info&quot; onclick=&quot;this.value='Please wait...'&quot; style=&quot;font-family : Arial, Helvetica, sans-serif; font-weight : bold; font-size : 12px; width: 508px; height: 20px&quot;> Sylvano
dsylvano@hotmail.com
 
Or patterns might be easier:
Code:
var PatternsDict = new Object();
PatternsDict.emailPat = /.*@.*\..*/;  
var isValidEmail = PatternsDict.emailPat.exec(TheValue); 
if(!isValidEmail) {
    var returnStr;
    returnStr = &quot;The email address field is invalid, this field is required in order to submit this form.  Please try again!&quot;;
    alert(returnStr);
    return false;
    }
See faq216-1165 for more patterns and a more all-encompassing script.
 
thanks both,
Sylvano - what message gets displayed if false is returned?
 
the default error message is something like: &quot;Error in email text...&quot;; if you want to customize it, add the message attribute in the cfinput tag, e.g.:

<cfinput type=&quot;text&quot; name=&quot;email&quot; size=&quot;30&quot; maxlength=&quot;30&quot; onvalidate=&quot;validEmail&quot; value=&quot;&quot; onfocus=&quot;this.select()&quot; message=&quot;The email you have entered is invalid.&quot;>
Sylvano
dsylvano@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top