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 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jan 2004
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    RegEx in search() method ... HELP!



    In the example above, Part1 should be a valid part number even though the item it's validating against has a lower case "p" vs. the value entered, which has an upper case "P". Part2 should be considered invalid since it's not in select box list.

    Clicking the “Calculate Estimate” button will run a validation script (code below) to validate that the zip code entered is valid and that the part entered is also valid. I do this by building a string, output from a database (using ColdFusion, thus the <cfoutput> tags in the code below) where each value is seperated by a comma (a list). Since all US ZIP Codes are 5 digits, I don’t have to worry about the search finding "duplicates", or part of the value. However, when using the same methodolgy with the Part Number validation, a problem occurs. "112100" is found because it’s part of Part Number "112100p", "112100US", etc., but 112100 IS NOT a valid part number.

    I have overcome this limitation by adding the comma as part of the sub-string that I am searching for, thus making the number unique. Now "112100," is not found in the following string: "112100p,112100US,112100N,...". However, my problem now is that the search() method seems to be case-sensitive. Since the people using the form might not enter the letter portion of part numbers in the proper case. So, a search for "112100P" isn't found in the string "112100p,112100US,112100N,...". I need the search method to run a
    case-insensitive search. How would I do this?
    Code:
    function ValidateFields() {	// Validate all required fields before submitting form
    	var vTotal = document.frmCalc.TotalPiecesHidden.value;
    	var validZip = "<cfoutput query='GetVPostalCode'>#postalCode#,</cfoutput>";
    	var validPart = "<cfoutput query='GetVParts'>#partID#,</cfoutput>";
    	var vZipCode = document.frmCalc.postalCode.value;
    	// Validate Postal Code
    	if (isNaN(vZipCode)) {
    		alert('Postal Code must be numeric values only.');
    		document.frmCalc.postalCode.focus();
    		document.frmCalc.postalCode.select();
    		return false;
    	}else{
    		if (vZipCode.search(/^\d{5}$/)){
    			alert('Postal Code must be 5 digits.');
    			document.frmCalc.postalCode.focus();
    			document.frmCalc.postalCode.select();
    			return false;
    		}else{
    			if (validZip.search(vZipCode) == -1){
    				alert('Invalid Postal Code, please check and try again.');
    				document.frmCalc.postalCode.focus();
    				document.frmCalc.postalCode.select();
    				return false;
    			}
    		}
    	}
    	// Validate Part Number
    	for (i=1; i<=vTotal; i++) {
    		var vPart = document.frmCalc['Part'+i].value;
    		if (vPart == ''){
    			alert("You must enter a part number.");
    			document.frmCalc['Part'+i].focus();
    			return false;
    			break;
    		}else{
    			var vPart = document.frmCalc['Part'+i].value + ',';
    			if (validPart.search(/ivPart/) == -1){
    				alert('Invalid part number, please check and try again.');
    				document.frmCalc['Part'+i].focus();
    				document.frmCalc['Part'+i].select();
    				return false;
    				break;
    			}else{
    				return true;
    			}
    		}
    	}
    }
    Last edited by FastCougar; 04-23-2004 at 05:57 PM.

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Assuming your code is correct, you merely want to do a search with case insensitivity.

    This should do the trick:
    Code:
    validPart.search(/ivPart/i)
    Hope that helps,
    Sadiq.

  • #3
    Regular Coder
    Join Date
    Jan 2004
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That doesn't work. Using the following code
    Code:
    if (validPart.search(vPart) == -1){
    	alert('Invalid part number, please check and try again.');
    "112100" is found to be invalid, which is good because it is.
    "112100p" is found to be valid, which is good because it is.
    "112100P" is found to be invalid, which is bad because only case is different.

    Using the code that you supplied in this code
    Code:
    if (validPart.search(/ivPart/i) == -1){
    	alert('Invalid part number, please check and try again.');[/
    All entries, even "valid" part numbers are being alerted as "invalid". So:
    "112100" is found to be invalid, which is good because it is.
    "112100p" is found to be invalid, which is bad because it is valid.
    "112100P" is found to be invalid, which is bad because it SHOULD be considered valid.

    I think that the problem lies in the fact that I am searching a variable "vPart" and adding the case logic to the RegEx is causing it to look for the variable as a literal "vPart" instead of the javascript variable which should translate to "112100,". I'm lost on this. Obviously, I can do all the validation on the server end, but I would prefer to do it on the client end instead to speed things up and to conserve bandwidth.

  • #4
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, what's ivPart? I think it should be vPart..

    Try one of the following:
    Code:
    validPart.search(/vPart/i)
    vPart.search(/validPart/i)
    One of those should work I think...

    Sadiq.

  • #5
    Regular Coder
    Join Date
    Jan 2004
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK, let me try to clear up some of the confusion:

    Since the user can input part numbers that can be sub-strings of other part numbers, I need to ensure that all part number that they enter is "unique" and not a substring of the search string (comma seperated list). So, like in the original code, I am adding a comma to their input value and I then search the sting for that value.

    I create the list string:
    Code:
    var validPart = "<cfoutput query='GetVParts'>#partID#,</cfoutput>";
    Here I search the string for the entered value appended with a comma:
    Code:
    var vPart = document.frmCalc['Part'+i].value + ',';
    	if (validPart.search(/ivPart/) == -1){
    		alert('Invalid part number, please check and try again.');
    	}
    So, vPart is the part that they entered appened with a comma. Say they entered 112100. I basically make this a unique value by searching for "112100," <-- notice the comma. The string that I am searching is a comma seperated list. Here is a partial snippet of that string (list) "112100p,112100N,112100US,". So, searching for 112100 without the comma appended will return all 3 of the above becuase it's part of all 3. I don't want this, so I append the comman. Searching with the comma appended will return -1 and thus error out, which is what I want. That much is working!

    However, if the user enters "112100P" <--- notice capitalized "P", that is not being found because of the case senetivity. I beginning to think that I should just uppercase the values being output into the string and then uppercase the values that the user enters, that way I am always comparing apples to apples.
    Last edited by FastCougar; 04-23-2004 at 08:29 PM.

  • #6
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I recall reading somewhere about regex and a \b which is a boundary.

    So if you had a string: abcd,abc and you split it by the comma to give you two strings, and then searched for /\babc\b/ it should reject abcd and accept abc.

    Maybe that will help you?

    Sadiq.

  • #7
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    FastCougar, you got the wrong gist out of Sadiq's correction there.

    Originally, he typo'd with validPart.search(/ivPart/i) when really he meant just validPart.search(/vPart/i) -- notice the very, very subtle difference? The first i, not the second, was removed. Let me break it down.

    validPart.search( -- okay, we're searching validPart for the argument to follow

    / -- hey, hey, that's the beginning of a regular expression

    vPart -- looks like that's what we're trying to find, cool, cool

    /i -- okay, so it's now a CASE-INSENSITIVE regular expression. Perfect!

    ); -- end of command.

    I saw in your reply above Sadiq's last reply that you *nearly* corrected the problem in the script; however, you dropped the wrong i. Try it again with validPart.search(/vPart/i); and you really oughta be golden.

  • #8
    Regular Coder
    Join Date
    Jan 2004
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Figured out a solution ... went with the apples to apples approach:

    Build the string (note the ColdFusion function UCase() ... makes everything upper case):
    Code:
    var validPart = "<cfoutput query='GetVParts'>#UCase(partID)#,</cfoutput>";
    Manipulate the user's entry to be all uppercase with an appended comma:
    Code:
    var vPart = document.frmCalc['Part'+i].value + ',';
    	vPart = vPart.toUpperCase();
    	if (validPart.search(vPart) == -1){
    		alert('Invalid part number, please check and try again.')
    	}
    This is now working just as I need it without using RegEx

  • #9
    Regular Coder
    Join Date
    Jan 2004
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Choopernickel
    FastCougar, you got the wrong gist out of Sadiq's correction there.

    Originally, he typo'd with validPart.search(/ivPart/i) when really he meant just validPart.search(/vPart/i) -- notice the very, very subtle difference? The first i, not the second, was removed. Let me break it down.

    validPart.search( -- okay, we're searching validPart for the argument to follow

    / -- hey, hey, that's the beginning of a regular expression

    vPart -- looks like that's what we're trying to find, cool, cool

    /i -- okay, so it's now a CASE-INSENSITIVE regular expression. Perfect!

    ); -- end of command.

    I saw in your reply above Sadiq's last reply that you *nearly* corrected the problem in the script; however, you dropped the wrong i. Try it again with validPart.search(/vPart/i); and you really oughta be golden.
    Thanks, but I already tried that as well and it's prompting at everything entered as Invalid when I know it's not. Somehow RegEx is getting dicked up. My solution is working just fine though. Thanks everyone for your help one this!
    Last edited by FastCougar; 04-23-2004 at 10:21 PM.


  •  

    Posting Permissions

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