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

validate textbox

Status
Not open for further replies.

huobaji

Technical User
Sep 20, 2010
23
0
0
US
I have a form that is validating textboxs that are empty.

textbox name

emplid
firstname
lastname

Can someone help with a problem I have

If I have a value in emplid then I don't want to validate firstname lastname. if I have a value in firstname and lastname Then I don't want to validate emplid

Can someone help me with this?
thanks in advance

I have the sample code I working with here

<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(EMID,"Input EMPLID!")==false)
{EMID.focus();return false;}

if (validate_required(firstname,"Input first name!")==false)
{firstname.focus();return false;}

if (validate_required(lastname,"Input last name!")==false)
{lastname.focus();return false;}

}
}
 
you need to check the values of your inputs, before running the validations and act accordingly. If EMID is not empty validate it. If it s empty, and the other 2 are filled in validate them.

Code:
with (thisform)
  {
[red]
if(EMID.value!=""){[/red]
        if (validate_required(EMID,"Input EMPLID!")==false)
  {EMID.focus();return false;}
[red]}[/red]

[red]else if(EMID.value=="" && (firstname.value!="" && lastname.value!="")){[/red]
  if (validate_required(firstname,"Input first name!")==false)
  {firstname.focus();return false;}
  
    if (validate_required(lastname,"Input last name!")==false)
  {lastname.focus();return false;}
[red]}[/red]
[code]

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown. 

Behind the Web, Tips and Tricks for Web Development. 
[URL unfurl="true"]http://behindtheweb.blogspot.com/[/URL]
 
I would limit the user input rather than validate which boxes are contextually correct. your current scenario requires complex validation logic and the user could be confused about which text boxes are required.

could you instead
1. ask the user how they want to locate the employee: by id or by name
2. then provide the appropriate input fields
3. validate the input fields (basically are all fields not null)
4. submit request

I would also recommend using a validation framework, rather than rolling your own. the framework will manage wiring everything together all you need to provide are the validation rules.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
I'd take Jason's a step further: use two forms, one with a "search by ID" button and the other with "search by Name" button, where each button is positioned directly below the relevant input fields. This should make it clear which of the field(s) the user should fill in without having to provide any long-winded explanatory text.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top