SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Phone Number Validation and Masking

    #######UPDATE#######
    After doing some research it appears that the only keys that I can press in the phone number fields are numbers which means the backspace and delete key have been disabled. The only question is, how do I enable them?

    I've taken over a web property and I'm in need of some guidance in regards to a web form and the JavaScript validation. I did not write the script and consider myself a JavaScript newbie.

    The page can be found here:
    http://www.rasmussen.edu/contact-us/...duleavisit.asp

    The issues lies with the home phone and work phone input fields. I do know that regular expressions are being used and that there is some kind of masking going on for the phone number. However, in Firefox when I fill out the phone number it does not allow me to change the phone number once entered nor will it advance to the next input field when I press the tab key. It works fine in IE6. Any help or advice is appreciated. Thanks.

    Here is the JavaScript:
    Code:
    // JavaScript Document
    	function validate(){
    	if (document.rasmussen.campus.value=="0"){
    			alert('Please select a Campus.');
    			document.rasmussen.campus.focus();
    			return false;
    		}
    		
    		if (document.rasmussen.program.value=="-1"){
    			alert('Please select a Program');
    			document.rasmussen.program.focus();
    			return false;
    		}
    		if (document.rasmussen.fname.value==""){
    			alert('Please enter your First Name');
    			document.rasmussen.fname.focus();
    			return false;
    		}
    		if (document.rasmussen.lname.value==""){
    			alert('Please enter your Last Name');
    			document.rasmussen.lname.focus();
    			return false;
    		}
    		if (document.rasmussen.address.value==""){
    			alert('Please enter your Address');
    			document.rasmussen.address.focus();
    			return false;
    		}
    		if (document.rasmussen.city.value==""){
    			alert('Please enter your City');
    			document.rasmussen.city.focus();
    			return false;
    		}
    		if (document.rasmussen.state.value=="0"){
    			alert('Please enter your State');
    			document.rasmussen.state.focus();
    			return false;
    		}
    		if(document.rasmussen.zip.value.search(/^\d{5}([\-]\d{4})?$/)==-1)
       {
          alert("The zip code you entered is not valid.\r\nPlease enter a 5 digit or a 10 digit zip code.");
    	  document.rasmussen.zip.focus();
          return false;
       }
    		
    		if (document.rasmussen.home_phone.value==""){
    			alert('Please enter a Home Phone');
    			document.rasmussen.home_phone.focus();
    			return false;
    		}
    		if(document.rasmussen.home_phone.value.search(/\d{3}\-\d{3}\-\d{4}/)==-1)
       {
          alert("The home phone number you entered is not valid.\r\nPlease enter a phone number with the format xxx-xxx-xxxx.");
    	  document.rasmussen.home_phone.focus();
          return false;
       }
    		if (document.rasmussen.work_phone.value==""){
    			alert('Please enter a Work Phone');
    			document.rasmussen.work_phone.focus();
    			return false;
    		}
    		if(document.rasmussen.work_phone.value.search(/\d{3}\-\d{3}\-\d{4}/)==-1)
       {
          alert("The work phone number you entered is not valid.\r\nPlease enter a phone number with the format xxx-xxx-xxxx.");
          document.rasmussen.work_phone.focus();
    	  return false;
       }
    		if (document.rasmussen.email.value==""){
    			alert('Please enter your Email');
    			document.rasmussen.email.focus();
    			return false;
    		}
    		if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(document.rasmussen.email.value)){
    		alert('Please enter a valid email address');
    			document.rasmussen.email.focus();
    			return false;
    }
    		if (document.rasmussen.gender.value=="0"){
    			alert('Please select your gender');
    			document.rasmussen.gender.focus();
    			return false;
    		}
    		if (document.rasmussen.start_date.value=="0"){
    			alert('Please tell us when you want to start your classes');
    			document.rasmussen.start_date.focus();
    			return false;
    		}
    		if (document.rasmussen.howhear.value=="0"){
    			alert('Please select How did you hear about us?');
    			document.rasmussen.howhear.focus();
    			return false;
    		}
    		return true;
    	}
    	function autoMask(field, event, sMask) {
            
    
            var KeyTyped = String.fromCharCode(getKeyCode(event));
            var targ = getTarget(event);
            keyCount = targ.value.length;
    
    	if(keyCount == sMask.length)
    	{
    		return false;
    	}
          if ((sMask.charAt(keyCount+1) != '#') && (sMask.charAt(keyCount+1) != 'A' ) )
          {
             field.value = field.value + KeyTyped + sMask.charAt(keyCount+1);
             return false;
          }
    
            if (sMask.charAt(keyCount) == '*')
                    return true;
    
            if (sMask.charAt(keyCount) == KeyTyped)
            {
                    return true;
            }
    
            if ((sMask.charAt(keyCount) == '#') && isNumeric(KeyTyped))
               return true;
    
            if ((sMask.charAt(keyCount) == 'A') && isAlpha(KeyTyped))
             return true;
    
          if ((sMask.charAt(keyCount+1) == '?') )
          {
             field.value = field.value + KeyTyped + sMask.charAt(keyCount+1);
             return true;
          }
          if (KeyTyped.charCodeAt(0) < 32) return true;
        return false;
    }
     function getTarget(e) {
      // IE5
       if (e.srcElement) {
            return e.srcElement;
       }
        if (e.target) {
            return e.target;
       }
     }
    
      function getKeyCode(e) {
     //IE5
     if (e.srcElement) {
            return e.keyCode
     }
      // NC5
      if (e.target) {
       return e.which
      }
     }
    
     function isNumeric(c)
    {
            var sNumbers = "01234567890";
            if (sNumbers.indexOf(c) == -1)
                    return false;
            else return true;
    
    }
    
    function isAlpha(c)
    {
            var lCode = c.charCodeAt(0);
            if (lCode >= 65 && lCode <= 122 )
              {
                    return true;
             }
            else
            return false;
    }
    
    function isPunct(c)
    {
            var lCode = c.charCodeAt(0);
            if (lCode >= 32 && lCode <= 47 )
              {
                    return true;
             }
            else
            return false;
    
    }
    Last edited by grantnovey; Feb 27, 2008 at 14:30.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    It's the autoMask function causing you grief.
    Specifically, the line "if(keyCount == sMask.length)"
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your response. I'm still unsure as to how to go about fixing the issue. I kind of knew where the issue lied but unsure as to how to fix it. Any other responses would be appreciated.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Move this line above the one I mentioned before.

    Code javascript:
          if (KeyTyped.charCodeAt(0) < 32) return true;
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very gracious of you. I'm so appreciative of this forum especially for a JavaScript newbie like me.


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
  •