Results 1 to 2 of 2

Thread: Javascript Help validateForm()

  1. #1
    Join Date
    May 2007
    Location
    UK
    Posts
    4,007
    Mentioned
    1 Post(s)
    Quoted
    12 Post(s)

    Default Javascript Help validateForm()

    The function

    Code:
    <script type='text/javascript'>
    		function validateForm()
    {
    alert("We are called");
    var x=document.forms["formbox"]["strLeadFirstName"].value;
    alert("form name:  " + x);
    if (x==null || x=="")
      {
      alert("First name must be filled out");
      return false;
      }
    var x=document.forms["formbox"]["strLeadPhone1"].value;
    if (x==null || x=="")
      {
      alert("Atleast 1 Phone Number must be filled out");
      return false;
      }
    var x=document.forms["formbox"]["strLeadEmail"].value;
    var atpos=x.indexOf("@");
    var dotpos=x.lastIndexOf(".");
    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
      {
      alert("Not a valid e-mail address");
      return false;
      }
    }
    </script>
    How it is called:
    Code:
    <form name="formbox" action="http://active.flg360.co.uk/api/APIHTTPPost.php" method="post" id="lead_form" onSubmit="javascript:return validateForm()">
    Tried debugging with Bart on IRC but "We are called" is called and alerts but the others don't work...

    Any help?

    -Boom

  2. #2
    Join Date
    Feb 2007
    Location
    Colorado, USA
    Posts
    3,716
    Mentioned
    51 Post(s)
    Quoted
    624 Post(s)

    Default

    <input type="submit" name="whatever" value="Submit" onClick='javascript:return validateForm()'>
    <input type="submit" name="whatever" value="Submit" onClick='javascript:return validateForm(this.form)'>

    That's the type of stuff I use, the second one is close to copy paste of what I use (just changed the name field) it executes the java right when they click submit, and it executes and pops up with the errors or what not before it triggers the PHP action for the entire form

    my javascript is weak as hell though, so I may have understood what you're doing wrong



    Code:
    <Script language='JavaScript'>
    function validate_form(fm)
    {
    	if(fm.first.value=="")
    	{
    		alert("Please input your first name.");
    		fm.first.focus();
    		return false;
    	}
    	if(fm.last.value=="")
    	{
    		alert("Please input at least 1 letter of last name.");
    		fm.last.focus();
    		return false;
    	}
    	if(fm.company.value=="")
    	{
    		alert("Please input a Company Name");
    		fm.company.focus();
    		return false;
    	}
    	if(fm.email.value =="")
    	{
    		alert("Please input an email");
    		fm.email.focus();
    		return false;
    	}
    	if(fm.email.value!="")
    	{
    		if (EmailCheckjs(fm.email.value) == false)
    		{
    			alert("Invalid E-mail Address");
    			fm.email.focus();
    			return false;
    		}
    	}
    	if(fm.message.value=="")
    	{
    		alert("Please input a message");
    		fm.message.focus();
    		return false;
    	}	
    }
    
    function EmailCheckjs(argvalue)
    {
    	if (argvalue.indexOf(" ") != -1)
    	  return false;
    	else if (argvalue.indexOf("@") == -1)
    	  return false;
    	else if (argvalue.indexOf("@") == 0)
    	  return false;
    	else if (argvalue.indexOf("@") == (argvalue.length-1))
    	  return false;
    	var arrayString = argvalue.split("@");
    	if(arrayString.length > 2)
    	  return false;
    	if (arrayString[1].indexOf(".") == -1)
    	  return false;
    	else if (arrayString[1].indexOf(".") == 0)
    	  return false;
    	else if (arrayString[1].charAt(arrayString[1].length-1) == ".") {
    	  return false;
    	}
      return true;
    
    }
    </script>

    you can use that if you want, it's OLD like 8 years old or so, I've just reused it since.. because it works, and changed it as needed
    Last edited by grats; 02-23-2013 at 01:25 PM.
    The only true authority stems from knowledge, not from position.

    You can contact me via matrix protocol: @grats:grats.win or you can email me at the same domain, any user/email address.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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