Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jan 2004
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    .search HELP!!!!

    I need to search for the exact string entered into a text box to see if it's part of a list. The code below works, but if I enter "112100", it's not balking because this is part of "112100N","112100P", etc. I need to find an occurance of "112100" by itself and if it's not found, error out.

    Code:
    function ValidatePart() {
    	var validPart = "112100EU,112100N,112100p,112100R,112100US";
    	// Validate Part Number
    	for (i=1; i<=vTotal; i++) {
    		if (document.frmCalc['Part'+i].value == ''){
    			alert("You must enter a part number.");
    			document.frmCalc['Part'+i].focus();
    			return false;
    			break;
    		}else{
    			if (validPart.search(document.frmCalc['Part'+i].value) == -1){
    				alert('Invalid part number, please check and try again.');
    				document.frmCalc['Part'+i].focus();
    				document.frmCalc['Part'+i].select();
    				return false;
    				break;
    			}
    		}
    	}
    }
    What am I doing wrong?

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You are searching within a string to see if anywhere within the string your substring is present.

    I would use an array and the match method to loop thru the array and check if a match is present, if not then do your alert and return false. Although, you could also do this with the string you are currently using, you would have to split the string at the commas to make an array. Therefore, starting with an array is easier to code and shorter.

    Below is an example, I did not use your multiple inputs and check if any of them were empty because quite frankly, I do not understand the theory. Do all of the inputs need to be filled out?

    Anways here is the example:
    Code:
    <script type="text/javascript">
     <!--//
      function ValidatePart(){
       var validPart = new Array("112100EU","112100N","112100p","112100R","112100US");
        for(partCount=0; partCount<validPart.length; partCount++){ 
          if(document.frmCalc['Part1'].value.match(validPart[partCount])){
             alert('Valid part number: '+validPart[partCount]);
             return true;
          }
    
    
          else if(partCount == validPart.length-1){
              alert('Invalid part number, please check and try again.');
              document.frmCalc['Part1'].focus();
              document.frmCalc['Part1'].select();
              return false;
          }
        }
      }
     //-->
    </script>
    </head>
    
    <body>
    <form name="frmCalc">
    <input type="text" name="Part1" value="112100"><br>
    <input type="button" value="Test" onclick="ValidatePart()">
    </form>
    .....Willy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •