SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist Brandon Luhring's Avatar
    Join Date
    Apr 2002
    Location
    IN, USA
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Quick code check -form validation

    Hey guys!

    Can anyone spot an error that I'm overlooking in the code below? The original formAction() function worked fine, until I added the validation sequence...

    Code:
    function formAction(SubmitType,ProdID,OptionNum,OptionPrice){
    		if (ValidatePage()){
    			document.forms[0].submitType.value = SubmitType;
    			document.forms[0].itemInQuestion.value = ProdID;
    			document.forms[0].OptionNumber.value = OptionNum;
    			document.forms[0].OptionPrice.value = OptionPrice;
    			document.forms[0].submit();
    		}
    	}
    	
    	function ValidatePage(){
    		Validated = true;
    		
    		for (i=0;i<document.forms[0].elements.length;i++) {
    			fieldName = document.forms[0].elements[i].name;
    			fieldValue = document.forms[0].elements[i].value;
    			if (!fieldName.match('Quantity') == null) {
    				document.forms[0].elements[i].value = fieldValue.replace(',', ''); //make sure there are no commas in the number
    				
    				if (isNaN(fieldValue)) {
        				ItemIDNumber = fieldName.replace('Quantity', ''); //leaves only the ID Number
        				document.forms[0].elements[i].focus();
        				document.forms[0].elements[i].select();
        				ErrorCounter += 1;
        				LastErrorID = ItemIDNumber;
        				stringOfErrors += '\t&middot; ' + ItemIDNumber + '\n';
        				Validated = false;
        			}
    			}
    		}
    		
    		if (Validated){
    			return true;
    		}else{
    			if (ErrorCounter > 1){
    				finalErrorString = 'The following products\' quantities are not valid numbers:\n\n';
    				finalErrorString += stringOfErrors + '\n';
    				finalErrorString += 'Please enter numeric values and try again.';
    			}else{
    				finalErrorString = 'The quantity for product ' + LastErrorID + ' is not a valid number.\n\n';
    				finalErrorString += 'Please enter a numeric value and try again.';
    			}
    			alert(finalErrorString);
    			
    			return false;
    		}
    	}
    Thanks for any help!

    Brandon

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This looks odd:

    if (!fieldName.match('Quantity') == null) {

    Might try

    if (fieldName.match('Quantity') != null) {

    Also - I think String.replace() always takes a regex as its first argument.

    HTML here?

    stringOfErrors += '\t&middot; ' + ItemIDNumber + '\n';

    Speaking of errors...do you have some kind of error reporting turned on? Foolhardy to script without it.
    Last edited by adios; Nov 4, 2003 at 12:02.
    ::: certified wild guess :::

  3. #3
    SitePoint Evangelist Brandon Luhring's Avatar
    Join Date
    Apr 2002
    Location
    IN, USA
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Adios-

    Thanks for the help! With your comments and some other tweaks its working. ...about the error warnings--I'm coding on a Mac now with little more than a text editor, I looked for the script warnings checkbox before but couldn't find it on Mac IE... I need to really sit down and find it sometime.

    Anyway, here is the code that is now working (Thanks again!):

    Code:
    	function formAction(SubmitType,ProdID,OptionNum,OptionPrice){
    		if (ValidatePage()){
    			document.forms[0].submitType.value = SubmitType;
    			document.forms[0].itemInQuestion.value = ProdID;
    			document.forms[0].OptionNumber.value = OptionNum;
    			document.forms[0].OptionPrice.value = OptionPrice;
    			document.forms[0].submit();
    		}
    	}
    	
    	function ValidatePage(){
    		Validated = true;
    		ErrorCounter = 0;
    		stringOfErrors = '';
    		
    		for (i=0;i<document.forms[0].elements.length;i++) {
    			document.forms[0].elements[i].value = document.forms[0].elements[i].value.replace(',', ''); //make sure there are no commas in the number
    			
    			fieldName = document.forms[0].elements[i].name;
    			fieldValue = document.forms[0].elements[i].value;
    			if (fieldName.match('Quantity') != null) {
    				if (isNaN(fieldValue)) {
        				ItemIDNumber = fieldName.replace('Quantity', ''); //leaves only the ID Number
        				document.forms[0].elements[i].focus();
        				document.forms[0].elements[i].select();
        				ErrorCounter += 1;
        				LastErrorID = ItemIDNumber;
        				stringOfErrors += '     \xB7 ' + ItemIDNumber + '\n';
        				Validated = false;
        			}
    			}
    		}
    		
    		if (Validated){
    			return true;
    		}else{
    			if (ErrorCounter > 1){
    				finalErrorString = 'The following products\' quantities are not valid numbers:\n\n';
    				finalErrorString += stringOfErrors + '\n';
    				finalErrorString += 'Please enter numeric values and try again.';
    			}else{
    				finalErrorString = 'The quantity for product ' + LastErrorID + ' is not a valid number.\n\n';
    				finalErrorString += 'Please enter a numeric value and try again.';
    			}
    			alert(finalErrorString);
    			
    			return false;
    		}
    	}

  4. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For the love of Pete, use a reference!
    Code:
    function formAction(SubmitType,ProdID,OptionNum,OptionPrice){
    	if (ValidatePage()){
    		document.forms[0].submitType.value = SubmitType;
    		document.forms[0].itemInQuestion.value = ProdID;
    		document.forms[0].OptionNumber.value = OptionNum;
    		document.forms[0].OptionPrice.value = OptionPrice;
    		document.forms[0].submit();
    	}
    }
    Should be
    Code:
    function formAction(SubmitType,ProdID,OptionNum,OptionPrice){
    	var f = document.forms[0];
    	if (ValidatePage()){
    		f.submitType.value = SubmitType;
    		f.itemInQuestion.value = ProdID;
    		f.OptionNumber.value = OptionNum;
    		f.OptionPrice.value = OptionPrice;
    		f.submit();
    	}
    }
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  5. #5
    SitePoint Evangelist Brandon Luhring's Avatar
    Join Date
    Apr 2002
    Location
    IN, USA
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by beetle
    For the love of Pete, use a reference!
    well, of course! ...*cough*... I mean what idiot wouldn't??

    Thanks, it looks much cleaner now, of course.


    B

  6. #6
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not only cleaner, but probably 10x faster
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





Bookmarks

Posting Permissions

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