SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Alert Disappeared

    Hi,guys! I have a javascript function to validate phone number.However,when invalid number entered, alert didn't prompt out,all fileds are reset instead.Thanks

    HTML Code:
    <script type="text/javascript">
    
    function validate_required(thisForm)
    { 
      Contact = document.enquiry.contact;
      var c = Contact.value.toString();
      var intCharCode;
      for(var i; i < c.length;i++)
        intCharCode = c.charCodeAt(i);
        if(intCharCode < 45 || intCharCode > 57)
        {alert("Please enter valid contact no");Contact.value = "";return false;}
         else {return true;}
    }
    </script>

  2. #2
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <script type="text/javascript">

    function validate_required(thisForm)
    {
    Contact = document.enquiry.contact;
    var c = Contact.value.toString();
    var intCharCode;
    for(var i=0; i < c.length;i++)
    intCharCode = c.charCodeAt(i);
    if(intCharCode < 45 || intCharCode > 57)
    {alert("Please enter valid contact no");Contact.value = "";return false;}
    else {return true;}
    }
    </script>
    You just declared a variable called i, didn't assign anything to it.

  3. #3
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, now the validation doesn't execute at all.

  4. #4
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The first two alerts displayed properly,only the third one is not displayed.What went wrong for this script?

    HTML Code:
    <script type="text/javascript">
    function validate_required(thisForm)
    { 
                 Name = document.enquiry.name;
    	  Company = document.enquiry.company;
    	  Contact = document.enquiry.contact;
    	  Email = document.enquiry.email;
    	  Enquiry = document.enquiry.enquiry;
                 apos = Email.value.indexOf("@");
    	  dotpos = Email.value.lastIndexOf(".");
    	 
    	  	  	  
          if(Name.value == "" || Company.value == "" || Contact.value == "" ||       Email.value == "" || Enquiry.value == "")
            {alert("Please enter all fields to submit"); return false;} 	
    		 
    else  if(apos < 1 || dotpos - apos < 2)
    	    {alert("Please enter valid Email");Email.value = "";return false;}
    	    else {return true;}
      
    	 var c = Contact.value.toString();
    	 var intCharCode;
    	 for(var i = 0; i < c.length;i++)
    	    intCharCode = c.charCodeAt(i);
    	 if(intCharCode < 45 || intCharCode > 57)
           {alert("Please enter valid contact no");Contact.value = "";return false;}
           else {return true;}
    
    
    }
    </script>

  5. #5
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The part of your code beginning with
    var c = Contact....

    never executes, because one of the conditions prior is always met and the function hits a RETURN.


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
  •