SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist dscriptor's Avatar
    Join Date
    Oct 2005
    Location
    in front of my computer
    Posts
    571
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question javascript vaildations.

    hi all,

    can somebody help me how to put validations in this set of info?
    please refer to this link:
    http://dancefun.com/store/product_in...products_id=62
    this is oscommerce for cruise passengers.

    i put a validations for this FIRST SET OF FORM. The set of forms depends on the numbers of passengers that the user select in the dropdown.

    the problem now is, what would be the logic in the way that the validations will only validate the set of form the user selected.(1 or 2 or 3 or 4 sets of forms).

    i hope this makes sense..
    thanks.

    anyone???
    happy is the man that finds wisdom....wisdom in {PHP}.


  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what would be the logic in the way that the validations will only validate the set of form the user selected
    Use the select's onchange event to get the value of the select, and then use a series of if-else statements (or a case statement) to pick out what their choice was and then take the appropriate action?

  3. #3
    SitePoint Evangelist dscriptor's Avatar
    Join Date
    Oct 2005
    Location
    in front of my computer
    Posts
    571
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    7stud,
    thanks for the reply...

    this is my validation for the first set only... please take a look..

    Code:
    function check_form(cart_quantity){
    var why = "";
    	why += checkFirstName(document.getElementById('FirstName(1)').value);
    	why += checkLastName(document.getElementById('LastName(1)').value);
    	why += checkGender(document.getElementById('Gender(1)').value);
    	why += checkAddress(document.getElementById('Address(1)').value);
    	why += checkCity(document.getElementById('City(1)').value);	
    	why += checkState1(document.getElementById('State(1)').value);	
    	why += checkZipcode(document.getElementById('Zipcode(1)').value);	
        why += checkCountry1(document.getElementById('Country(1)').value);
    	why += checkCountry(document.getElementById('Country(1)').value);
        why += checkCountryIfUS(document.getElementById('Country(1)').value);
    	why += checkBadge(document.getElementById('BadgeName(1)').value);	
    	why += checkBirth(document.getElementById('DateofBirth(1)').value);	
    	why += checkDayPhone(document.getElementById('DaytimePhone(1)').value);	
    	why += checkEmail(document.getElementById('Email(1)').value);	
        /*why += checkPhone(cart_quantity.phone.value);    
        why += checkUsername(cart_quantity.username.value);*/
      
       
        if (why != "") {
           alert(why);
           return false;
        } 
    return true;
    }
    function checkFirstName(strng){
    	var error = "";
    	if (strng== "") {
        error = "Please enter your First Name.\n";
    	} 
    	
    	return error;
    }
    function checkLastName(strng){
    	var error = "";
    	if (strng== "") {
        error = "Please enter your Last Name.\n";
    	} 
    	
    	return error;
    }
    
    function checkGender(strng){
    	var error = "";
    	if (strng== "") {
        error = "Please enter your Gender.\n";
    	} 	
    	return error;
    }
    
    function checkAddress(strng){
    	var error = "";
    	if (strng== "") {
        error = "Please enter your Address.\n";
    	} 	
    	return error;
    }
    
    function checkCity(strng){
    	var error = "";
    	if (strng== "") {
        error = "Please enter your City.\n";
    	} 	
    	return error;
    }
    
    function checkState1(strng){
    	var error = "";
    	if (strng== "") {
        error = "Please enter your State Code.\n";
    	} 	
    	return error;
    }
    function checkZipcode(strng){
    	var error = "";
    	if (strng== "") {
        error = "Please enter your Zipcode.\n";
    	} 	
    	return error;
    }
    function checkBadge(strng){
    	var error = "";
    	if (strng== "") {
        error = "Please enter your Badge Name.\n";
    	} 	
    	return error;
    }
    
    function checkCountry1(strng){
    	var error = "";
    	if (strng== "") {
        error = "Please enter your Country Code.\n";
    	} 	
    	return error;
    }
    
    function checkCountry(strng){
    	var error = "";	
    	
    	if (strng.length  > 2) {
        error = "Please use the two letter country code.\n";
    	} 
    	
    	return error;
    }
    
    function checkCountryIfUS(strng){
    	var error = "";
    	if (strng == "US" || strng == "us") {
    		//check for the length of state
    		
        	if (!checkState()) {
        		error = "Please use the two letter code for your State.\n";
    			document.getElementById('State(1)').focus =true;
    		}
    		
    	} 
    	return error;
    }
    
    function checkState(cart_quantity){
    
    	if (document.getElementById('State(1)').value != "") {				
    		if (document.getElementById('State(1)').value.length  > 2) {
    			/*alert("sobra");*/
    			return false;
    		} else {
    			return true;
    		}
    		
    	} else if (document.getElementById('State(1)').value == ""){
    		return false;
    	} else {
    		return true;
    	}
    	
    	return true;
    
    }
    
    function checkBirth(strng){
    	var error = "";
    	if (strng== "") {
        error = "Please enter the Date of your Birth in this format: mm/dd/yyyy.\n";
    	} else {	
    		if (!ValidateDate()){
       		//alert('error in date');
    		error = "There is an error in your Birth Date.\n";
    		 }
    	}
    	
    	return error;
    }
    
    function checkDayPhone(strng){
    	var error = "";
    	if (strng== "") {
        error = "Please enter your Daytime Phone.\n";
    	} 	
    	return error;
    }
    
    function checkEmail(strng){
    	var error="";
    	if (strng == "") {
    	   error = "You didn't enter an email address.\n";
    	}
    
        var emailFilter=/^.+@.+\..{2,3}$/;
        if (!(emailFilter.test(strng))) { 
           error = "Please enter a valid email address.\n";
        }
        else {
    //test email for illegal characters
           var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
             if (strng.match(illegalChars)) {
              error = "The email address contains illegal characters.\n";
           }
        }
    return error;    
    }
    
    	
    var dtCh= "/";
    var minYear=1900;
    var maxYear=2100;
    
    function ValidateDate(cart_quantity){
    	var dt= document.getElementById('DateofBirth(1)').value;
    	if (isDate(dt)==false){
    		//dt.focus()
    		return false;
    	} else {
        return true;
    	}
     }
    
    
    function isInteger(s){
    	var i;
        for (i = 0; i < s.length; i++){   
            // Check that current character is number.
            var c = s.charAt(i);
            if (((c < "0") || (c > "9"))) return false;
        }
        // All characters are numbers.
        return true;
    }
    
    function stripCharsInBag(s, bag){
    	var i;
        var returnString = "";
        // Search through string's characters one by one.
        // If character is not in bag, append to returnString.
        for (i = 0; i < s.length; i++){   
            var c = s.charAt(i);
            if (bag.indexOf(c) == -1) returnString += c;
        }
        return returnString;
    }
    
    function daysInFebruary (year){
    	// February has 29 days in any year evenly divisible by four,
        // EXCEPT for centurial years which are not also divisible by 400.
        return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
    }
    function DaysArray(n) {
    	for (var i = 1; i <= n; i++) {
    		this[i] = 31
    		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
    		if (i==2) {this[i] = 29}
       } 
       return this
    }
    
    function isDate(dtStr){
    	var daysInMonth = DaysArray(12)
    	var pos1=dtStr.indexOf(dtCh)
    	var pos2=dtStr.indexOf(dtCh,pos1+1)
    	var strMonth=dtStr.substring(0,pos1)
    	var strDay=dtStr.substring(pos1+1,pos2)
    	var strYear=dtStr.substring(pos2+1)
    	strYr=strYear
    	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
    	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
    	for (var i = 1; i <= 3; i++) {
    		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
    	}
    	month=parseInt(strMonth)
    	day=parseInt(strDay)
    	year=parseInt(strYr)
    	if (pos1==-1 || pos2==-1){
    		alert("The Birth date format should be : mm/dd/yyyy")
    		return false
    	}
    	if (strMonth.length<1 || month<1 || month>12){
    		alert("Please enter a valid month")
    		return false
    	}
    	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
    		alert("Please enter a valid day")
    		return false
    	}
    	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
    		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
    		return false
    	}
    	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
    		alert("Please enter a valid date for your Birth Date")
    		return false
    	}
    return true
    }
    this is a very long validation just for one passenger info?

    how about if the customer choose 3 or 4 passengers? how will i do it?

    please help me in this matter..

    thanks...
    happy is the man that finds wisdom....wisdom in {PHP}.


  4. #4
    SitePoint Evangelist dscriptor's Avatar
    Join Date
    Oct 2005
    Location
    in front of my computer
    Posts
    571
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    bump...can someone give me more ideas about this thing?

    thanks... really need your help...
    happy is the man that finds wisdom....wisdom in {PHP}.



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
  •