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

Form validation - checking for duplicate values in text fields

Status
Not open for further replies.

billdvldog

Programmer
Sep 6, 2001
43
I am very new to JavaScript, but know it's probably the best way to implement this requirement. The requirement is to prevent duplicate values from being entered into a number of text fields on a form and submitted. The actual number of text fields can vary depending on the number of statements in the database. What this page does is list the statements with a text box next to each statement with the order number as the value. The user can then update the order numbers and submit the form to update the database. What I'm trying to do is have a function that will prevent two statments from having the same order number on form submission.
 
You can have JavaScript ensure the order number is unique relative to the other orders on the page (providing that the user's browser has JavaScript enabled), but is your page going to show every order in the database? If not, then you wouldn't be able to compare the orders that are not on the page. You should include a server-side process as well.

Code:
function hasDups(){

  [green]// Create the array[/green]
  var myArray=[];

  [green]// Populate the array[/green]
  for(i=0;i<myForm.elements.length;i++){
    if(myForm.elements[i].type=='text'){
      myArray[myArray.length]=myForm.elements[i].value;
    }
  }

  [green]// Sort the array[/green]
  myArray.sort();

  [green]// Check for duplicates[/green]
  for(i=0;i<myArray.length-1;i++){
    if(myArray[i]==myArray[i+1]) return true;
  }
  return false;
}

Adam
 
Thanks Adam! I've got the server side process set up, just needed something on the client side for this particular requirement. And I am only checking the statements on the screen, not everything in the db. Thanks for your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top