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

Validating a hidden field 1

Status
Not open for further replies.

3112005

Technical User
Nov 28, 2005
58
US
I have a form that is broken out into 4 pages. Dependant on whether you select "Exempt" or "Non-Exempt" on page 3 you will have different choices on page 4.

How can I modify my script so that it only validates the fields that are being displayed.

In this case let's say that only the "Current Salary" field is displayed and validated.

Code:
<script type="text/javascript">

function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value=="")
  {alert(alerttxt);return false}
else {return true}
}
}
function validate_form(thisform)
{
with (thisform)
{
if (validate_required(curr_annual_salary,"CURRENT SALARY must be filled out!")==false)
  {curr_annual_salary.focus();return false
}
  
if (validate_required(new_annual_salary,"NEW ANNUAL SALARY must be filled out!")==false)
  {new_annual_salary.focus();return false
}

    return true; 
  }
}

</script>
<form ACTION="<%=MM_editAction%>" name="PAN4" id="PAN4" language="JavaScript" onsubmit="return ( msg() && validate_form(this) );" method="post">
 
how are you "displaying" or "hiding" them?

easiest method (based on your code) would be something like this (untested):

Code:
if ([red]curr_annual_salary && [/red]validate_required(curr_annual_salary,"CURRENT SALARY must be filled out!")==false)
  {curr_annual_salary.focus();return false
}



*cLFlaVA
----------------------------
[tt]mr. pibb + red vines = crazy delicious![/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
I am using a SQL statement to determine what fields are displayed or hidden.

Code:
<%
IF  WageType.Fields.Item("wage_type").Value = "Exempt Salaried" Then
%>

Display these fields....

<%
Else
%>

Display these fields....
 
I made then changes as you suggested to my script, but when I run through that page is give me an error when I try to submit...

Error: 'curr_annual_salary' is underdefined
 
the question really is, why are you doing some stuff based on vbscript and not other stuff?

if you're checking to see if the value is "Exempt Salaried", then why don't you do the same when generating your page, in the javascript section?

only validate the fields that you'll eventually display.



*cLFlaVA
----------------------------
[tt]mr. pibb + red vines = crazy delicious![/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
I'm not sure I understand your question and comment but I'll try and answer. I am by no means a programmer (I am a web designer) and have been trying to do the best I can to make this site work that was left for me to finish up when I was hired. From the begining it has been a mess, so I just tried to continue with what was there.

So I guess the answer to why I am doing some stuff in VBscript and others in Javascript is because that is the only way I know to make it work.

Are you saying form validation should be done in VBscript?

I was thinking I was going to have to include a VBscript with my JavaScript form validation to make this work.

Yes you are correct, I only want to validate the fields that I will eventually display.

Thank you.
 
regardless of all of this, you should ALWAYS validate your data server-side (vbscript) in addition to client-side (JavaScript). If a user has JS disabled, your validation will not occur and crap data could be sent to your database.


this worked for me:

Code:
if (elements['curr_annual_salary'] && !validate_required(elements['curr_annual_salary'],"CURRENT SALARY must be filled out!"))



*cLFlaVA
----------------------------
[tt]mr. pibb + red vines = crazy delicious![/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
I may be reading this wrong but it sounds to me like your actual question is how do you know which fields were displayed so you know which ones to test in your validation routine?

In your ASP code when you are building the page you can set a value for each field that was displayed and in your javascript check if the value was set and if it was then perform validation on that field.

There are really a lot of ways to handle it, it's just a matter of knowing which fields are displayed. This is also unclear however. Does "displayed" mean they do or do not exist on the page or that all fields exist but are shown/hidden based on the results in your ASP code?


At my age I still learn something new every day, but I forget two others.
 
cLFlaVA ---

Your code worked when kept the return false and return true statements below it, but it still submited my page after the pop-up validation box came up and I clicked ok.

I didn't know I was supposed to use your code exactly, so I tried it both with the return false and true statements and without. When I used it without it didn't work at all.

Code:
if (elements['curr_annual_salary'] && !validate_required(elements['curr_annual_salary'],"CURRENT SALARY must be filled out!")==false)
  {curr_annual_salary.focus();return false
}

    return true; 
  }
}

Can you tell me exactly what part of the code I need?
 
gah.

this should be your function:

Code:
function validate_form(thisform) {
    with (thisform) {
        if (elements['curr_annual_salary'] && !validate_required(elements['curr_annual_salary'],"CURRENT SALARY must be filled out!")) {
            elements['curr_annual_salary'].focus();
            return false;
        }
        if (elements['new_annual_salary'] && !validate_required(elements['new_annual_salary'],"NEW SALARY must be filled out!")) {
            elements['new_annual_salary'].focus();
            return false;
        }
    }
    return true;
}



*cLFlaVA
----------------------------
[tt]mr. pibb + red vines = crazy delicious![/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top