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

Validate Text

Status
Not open for further replies.

ashw

MIS
Jun 10, 2002
61
CA
HI,
I want the user to input http:// before entering any weblink address. If a user forgets, for example he put instead of , then on submit , either http:// should be inserted automatically before or only http:// should be inserted with cursor after it and an alert prompting the user to enter weblink address after the http://

I have written the following code but it add the weblink address even though first 7 char are not http://

<!---CF Includes Site Header, CSS Style Sheet and Side Menu--->
<cfinclude template=&quot;/Header.cfm&quot;>
<head>
<!--- Validate Web Link Description --->
<script language=&quot;javascript&quot;>
function validate(obj){
if(obj.weblinkdescription.value.length==0){
alert(&quot;Enter website description!&quot;);
obj.weblinkdescription.focus();
return false;
} else {
return true;
}
}
</script>

<!--- Text Validate Web link Address to http:// --->
<script language=&quot;javascript&quot;>
function validateWebAdd(weblinkaddress)
{
/*
This function will return true if the first 7 characters are equal to &quot;*/
if (weblinkaddress.substring(0, 6) != &quot; {
return false;
}
return true;

}
</script>
<title>CreateNewWebLink</title>

<!---CF Query Department database--->
<cfquery name=&quot;departmentsearch&quot; datasource=&quot;staffdirectory&quot;>
SELECT deptID, dept
FROM tbldepartment
</cfquery>
</head>
<body>
<!---CF Form Action (submits to weblinksubmit.cfm)--->
<cfform action=&quot;SubmitNewWeblink.cfm&quot; name=&quot;newweblink&quot; method=&quot;POST&quot; enctype=&quot;multipart/form-data&quot; onsubmit=&quot;return validate(newweblink)&quot;>
<table>
<th colspan=&quot;2&quot;>Create New Web Link</th>
<tr>
<td>Site Name</td>
<td>

<cfinput type=&quot;text&quot; name=&quot;weblinkName&quot; MESSAGE=&quot;Website Name is required!&quot; REQUIRED=&quot;Yes&quot;>


</td>
</tr>
<tr>
<td>Site Address</td>
<td>
<cfinput type=&quot;text&quot; name=&quot;weblinkaddress&quot;
MESSAGE=&quot;Website Address is required!&quot; REQUIRED=&quot;Yes&quot;>i.e. </td>
</tr>
<tr>
<td>Department</td>
<td>
<cfinput type=&quot;text&quot; name=&quot;weblinkdepartment&quot; MESSAGE=&quot;Website Address is required!&quot; value=&quot;#session.auth.dept#&quot; REQUIRED=&quot;Yes&quot;>
<!---CF Department Search Output--->

</td>
</tr>
<tr>
<td>Description</td>
<td>
<textarea name=&quot;weblinkdescription&quot; rows=&quot;6&quot; cols=&quot;60&quot;></textarea>

</td>
</tr>
<tr>
<td>Author</td>
<td>
<cfinput type=&quot;text&quot; name=&quot;weblinkauthor&quot; MESSAGE=&quot;Author Field is required!&quot; Value=&quot;#session.auth.Lastname#, #session.auth.FirstName#&quot; REQUIRED=&quot;Yes&quot;>
</td>
</tr>
<tr>
<td>&nbsp; </td>
<td>
<input type=&quot;submit&quot; value=&quot;Submit&quot;
onclick=&quot;validateWebAdd(document.forms[0].weblinkaddress.value)&quot;>

&nbsp;<input type=&quot;reset&quot; name=&quot;Reset&quot; value=&quot;Reset&quot;>

<input type=&quot;button&quot; name=&quot;Button&quot; value=&quot;Cancel&quot; onClick=&quot;window.location='weblinks.cfm';&quot;>

</td>
</tr>
</table>
</cfform>
</body>
<cfinclude Template=&quot;/footer.cfm&quot;>
 
ashw,
There look to be a couple of things wrong, actually.

The first is your validateWebAdd is only looking at the first 6 characters (the second parameter of substr is a count, not a character position), and
Code:
http://
is 7 characters long. Also, you have a semi-colon (;) in the IF statement that's breaking it. Thus, you'll want to change that IF to:
Code:
if (weblinkaddress.substring(0, 7) != &quot;[URL unfurl="true"]http://&quot;)[/URL]

Next, validateWebAdd now returns FALSE correctly... but it's returning it to the SUBMIT button (since that's where it's called on the onclick) not the FORM. So the form still thinks it's okay to proceed with the submit. You'll want to call validateWebAdd from the FORM's onSubmit event. Unfortunately, what this probably means is you'll need to go back to a standard FORM tag instead of a CFFORM... because I'm pretty sure that, even if you were able to add the onSubmit to the CFFORM, ColdFusion would overwrite it with it's own validation functions.

Hope it helps,
-Carl
 
Hi,
I have done the following,
<script language=&quot;javascript&quot;>
function validate(obj) {

if(obj.weblinkaddress.substring(0, 7) != &quot; {
alert(&quot;Web link address must have ' in it!&quot;);
weblinkaddress.focus();
return false;
}
if(obj.weblinkdescription.value.length ==0) {
alert(&quot;Enter Web Link Description!&quot;);
obj.weblinkdescription.focus();
return false;
}
return true;
}


<cfform action=&quot;SubmitNewWeblink.cfm&quot; name=&quot;newweblink&quot; method=&quot;POST&quot; enctype=&quot;multipart/form-data&quot; onsubmit=&quot;return validate(this);&quot;>

It gives javascript error that weblinkaddress is null or not an obj.
PLEASE can anyone tell me where am I goin wrong?
 
You're mixing metaphors.

weblinkaddress is the field object... what you need (for substring) is the string value that's in the field:

Code:
     if(obj.weblinkaddress.value.substring(0, 7) != &quot;[URL unfurl="true"]http://&quot;;)[/URL] {
          alert(&quot;Web link address must have '[URL unfurl="true"]http://'[/URL] in it!&quot;);
          obj.weblinkaddress.focus();
          return false;
     }
     if(obj.weblinkdescription.value.length ==0) {
          alert(&quot;Enter Web Link Description!&quot;);
          obj.weblinkdescription.focus();
          return false;
     }


Have you thought about doing the check on the backend? When the form data is submitted, you can have ColdFusion check to see if the value for weblinkaddress starts with if not, then prepend it before it does the insert into the database.

It would seem to be a lot more friendly.

Hope it helps,
-Carl
 
Hi, I tried, it won't work, I think I should go for the server side validation for http:// as u suggested
 
Yes... I just realized that you were still using a CFFORM in your last example.

Remember I said &quot;you'll need to go back to a standard FORM tag instead of a CFFORM... because I'm pretty sure that, even if you were able to add the onSubmit to the CFFORM, ColdFusion would overwrite it with it's own validation functions.&quot;. If you do a view source of your page once it's loaded, you'll see that's exactly what's happened.

CFFORM is not a valid HTML tag. When ColdFusion hits a CFFORM tag, all it's doing is rewriting it as a regular FORM tag with some extra javascript. Unfortunately it plugs all that javascript into the FORM tags obsubmit event. So your code is actually coming out like:

Code:
<FORM NAME=&quot;newweblink&quot; ACTION=&quot;thisform.cfm&quot; METHOD=POST
Code:
onSubmit=&quot;return _CF_checknewweblink(this)&quot;
Code:
 ENCTYPE=&quot;multipart/form-data&quot;>

or something similar. So your submit is never even seeing your validate function.

You would need to code your page with a FORM tag, rather than a CFFORM tag. One easy way to do this would be, since you already have the page coded with a CFFORM, view source and copy all the javascript and paste it into the head section of your page. Then switch out the CFFORM for a FORM. Now add your weblinkaddress validate to the _CF_checknewweblink() function in the code you just pasted (should be as simple as adding your if/then blocks before and after the ones that are already there).

Oh... and one other thing... that nagging &quot;;&quot; crept back into your first if/then block. Should be:

Code:
if(obj.weblinkaddress.value.substring(0, 7) != &quot;[URL unfurl="true"]http://&quot;)[/URL] {
    alert(&quot;Web link address must have '[URL unfurl="true"]http://'[/URL] in it!&quot;);
    obj.weblinkaddress.focus();
    return false;
}

that should do it for you... if you still want to go that way.


Hope it helps,
-Carl
 
HI,
I know its lot easier with FORM tag but I have used CFFORM and therefore now I am doing server side validation. like , I am letting weblinkaddress entered as say instead of and then checking and prepending http:// in in the access database . This is how I do it but still it shows error. can u take a look please? Please help, I need to finish it by today.

<!--- Server Side Validation for http:// before weblink address entered in the field --->
<CFIF LEFT(#FORM.NewWeblink, 7)# NEQ &quot;<!---If http:// is not there then prepend http:// in web link address --->
<cfquery datasource=&quot;weblink&quot; dbtype=&quot;ODBC&quot;>
UPDATE tblweblink
SET weblinkaddress = ' + weblinkaddress
WHERE weblinkID = #form.weblinkId#
</cfquery>
<CFLOCATION url=&quot;weblinks.cfm&quot;>
<CFELSE>
<!--- http:// is already entered by the user--->
<CFLOCATION url=&quot;weblinks.cfm&quot;>
</CFIF>
 
What's the error that you're getting?
There are any number of pure coding errors in the sample you posted (# pound signs not in the right place, errant ; semi-colons in lines, tags not closed properly). So I'm sure you're getting a great number of errors that have nothing to do with the validation itself.

In addition, it seems to me like it would be a lot easier (and better) to do the validation before the data ever gets into the database.

So, rather than an UPDATE, you'd do the validation before the INSERT.

Code:
<CFSET validweblink = trim(FORM.NewWeblink)>
<CFIF LEFT(validweblink, 7) NEQ &quot;[URL unfurl="true"]http://&quot;>[/URL]
   <!---If http:// is not there then prepend http:// in web link address --->
   <CFSET validweblink = &quot;[URL unfurl="true"]http://#validweblink#&quot;>[/URL]
</CFIF>

<!--- NOW insert the record --->
<cfquery datasource=&quot;weblink&quot; dbtype=&quot;ODBC&quot;>
INSERT INTO tblweblink
    (weblinkaddress,weblinkID,... etc. ...)
VALUES
    ('#validweblink#',#form.weblinkId#,... etc. ...)
</cfquery>

<CFLOCATION url=&quot;weblinks.cfm&quot;>


By the way... what are you going to do if someone enters &quot; That would still be a valid protocol. Or even &quot;ftp://&quot;? Or what if the user makes a typo and adds three &quot;/&quot;s - that would still pass your validation, but would be an invalid link. You might want a little more robust validation mechanism.

Something like:

Code:
<!--- set a default protocol (no slashes) --->
<CFSET protocol = &quot;http&quot;>
<!--- get a clean version of the submitted link --->
<!--- CFSET validweblink = trim(FORM.NewWeblink) --->
<CFSET validweblink = &quot;blah://[URL unfurl="true"]www.mentor.com/jkhjh/asdjhasdj/asjhasdjh/index.html&quot;>[/URL]

<!--- check to see if a protocol is indicated (by looking for a &quot;:&quot;
      as a list delimiter --->
<CFIF ListLen(&quot;#validweblink#&quot;,&quot;:&quot;) GT 1>
    <!--- if a &quot;:&quot; exists, break the link apart, separaing
	      the protocol from the rest of the link --->
	<CFSET protocol = lcase(ListGetAt(&quot;#validweblink#&quot;,1,&quot;:&quot;))>
	<CFSET validweblink = ListRest(&quot;#validweblink#&quot;,&quot;:&quot;)>
</CFIF>

<!--- remove any slashes at the beginning of the link --->
<CFLOOP CONDITION=&quot;Left(validweblink,1) EQ '/'&quot;>
	<CFSET validweblink = RemoveChars(validweblink,1,1)>
</CFLOOP>

<!--- only do the INSERT if the protocol is one that you want to accept.
      You can't assume just replacing the bad protocol with HTTP will 
	  do the trick. A submitted link of mailto:someone@somewhere.com
	  would resolve to [URL unfurl="true"]http://someone@somewhere.com[/URL] - which would not 
	  be valid --->
<CFIF protocol NEQ &quot;http&quot; AND protocol NEQ &quot;https&quot; AND protocol NEQ &quot;ftp&quot;>
	<!--- reassemble the link with the protocol and
	      proper format with &quot;:&quot; and slashes --->
	<CFSET validweblink = &quot;#protocol#://#validweblink#&quot;>
	
	
        <cfquery datasource=&quot;weblink&quot; dbtype=&quot;ODBC&quot;>
           INSERT INTO tblweblink
               (weblinkaddress,weblinkID,... etc. ...)
           VALUES
               ('#validweblink#',#form.weblinkId#,... etc. ...)
        </cfquery>

        <CFLOCATION url=&quot;weblinks.cfm&quot;>

<CFELSE>
    <P>You entered an invalid URL.</P>

    <P><a href=&quot;weblinks.cfm&quot;>Click here to return to the entry form</a></P>
</CFIF>

Or something like that.
Hope it helps,
-Carl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top