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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validating Free Form Text

Status
Not open for further replies.

Sherylj

Programmer
Jan 21, 2000
55
US
I have a very novice question. I've been out of the Cold Fusion business for more than two years so I've forgotten half of what I know. I know I've done this before and I think it was with either a CFIF or Javascript but can't find any code from the past. I need to prevent entry of all special characters in several cfinput boxes with the exception of the &. A quick snippet of code would be appreciated.
 
I don't have a snippet handy, but I think the areas you need to look at are CFFORM, CFINPUT, and validation with regular expressions.

With regular expressions you can tell the field exactly what is allowed and what is not.
 
Can you give me a quick example if I don't want to allow ! in this? I can do the rest from there I imagine.

<cfinput type="Text" name="serv_loc_name" required="yes" size="40" message="Service Location Name (Not Found) is a required field.">
 
Save the following as "Validate_Entry.js"

Code:
function requiredfields() {
    var form = document.getElementById("AddPerson");

    if (form.Name.value == "") {
        alert("Name is a required value");
        form.Name.focus ();
        return false;
    }

    if (form.Name.value!="") {
	var valid = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-.";
	var ok = "yes";
	var temp;
	for (var i=0; i<form.Name.value.length; i++) {
		temp = "" + form.Name.value.substring(i, i+1);
	        if (valid.indexOf(temp) == "-1") ok = "no";
	}
	if (ok == "no") {
		alert("Only alphabetic characters are allowed in names");
		form.Name.focus ();
	        return false;
	}
    }

    return true;

}

Then add the following to your FORM tag: "<SCRIPT LANGUAGE="JavaScript" SRC="validate_entry.js"></SCRIPT>"

(make sure to change the "AddPerson" part in the script to the NAME of YOUR form)

The line that begins "var valid = " in the script is where you define the valid characters.




 
Does this work if you want to validate only certain fields in the form or will it apply the validation rules for all cfinput's? I have some form inputs that will allow the special characters and some that will not.
 
The example above will validate for a form field called "Name". Just copy & paste and change that form field to your own to validate additional ones.
 
You could also try :

<cfinput type="Text" name="serv_loc_name" required="yes" size="40" message="Service Location Name (Not Found) is a required field." VALIDATE="regular_expression" PATTERN="[A-z0-9]+">

This should allow any upper or lower case alpha or numeric characters, and nothing else. I haven;t had time to test it though.

 
Unfortunately, it didn't like that.

Error Occurred While Processing Request
Error Diagnostic Information
CFInput

Supported validate rules are float, integer, date, time, eurodate, telephone, zipcode, creditcard, social_security_number

The error occurred while processing an element with a general identifier of (CFINPUT), occupying document position (71:18) to (71:199) in the template file


 
What version of CF are you running - in MX7 that should run just fine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top