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

javascript validation

Status
Not open for further replies.

gambhir

Programmer
Oct 17, 2000
15
US
anyone have any idea how to perform a validation where if a user does not enter a required input the cursor will in addition to the pop-up message, also move the cursor to that input field. My form is huge, and woule be cumbersome for the user to look for that field where they need to enter data in.

Also if a user checks a checkbox, is there I way I can make input fields madatory. If the checkbox is not checked those same fields on the same form are not mandatory.


Cheers for any suggestions,


Sahil
 
This is pretty simple using Javascript. To return focus to a required field:

alert("Field is required")
document.FormName.FieldName.focus()
document.FormName.FieldName.select()

return false

As for only validating if a checkbox is checked, something like this should work:

function ValidateForm()
{
if document.FormName.CheckboxName.checked
{
if (document.FormName.FieldName.value == "")
{
alert("Field is required")
document.FormName.FieldName.focus()
document.FormName.FieldName.select()
return false
}
}
return true
} Andrew
amayer@sonic.net
 
Are you incorporating JS into your CF form? If so, you can use the options available with CFINPUT. You can view them with the tag editor. This might be easier; especially if your users might have their JS turned off.
 
Yes,

I would like to use JS in my CFinput tags. I am able to use a few JS functions in cfinput like the message=""

I can not re-focus the cursor on that input where the error occured in CFinput, or I just don't know how.
 
Gambhir,

Have you tried activating the required field function within the CFInput tag? (ex:<cfinput Type = &quot;text&quot;
name = &quot;YourFieldName&quot;
maxlength=&quot;15&quot; required=&quot;yes&quot; message=&quot;This is a required field!&quot; Size = &quot;15&quot;>

Then, use something like the above JS to return the focus to the required field that has been left blank.



 
Yes I do use the message =&quot;this is required field within the CFinput tag&quot;

I thought about using an onerror in the cfinput tag, but was unsure as to the syntax of that onerror code(do i use onerror or onvalidate) within the CFinput tag.
 
Hmmmm...not sure about that one. Logic dictates &quot;on error&quot;. I would actually code it to perform a validation upon receiving focus. Then direct the cursor accordingly and if the conditions are met.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top