Link to home
Start Free TrialLog in
Avatar of needexpert
needexpert

asked on

Validation btn not working now?

I have read my code until I am blue in the face. I am doing a client side validation. I changed a couple of things but now I got something out of wake with my curly braces I think. Can someone please look at the code and tell me what I am doing wrong.  The check() function is where the problem is and the code was nicely formated until I pasted it here. Thanks for a fast reply.

<HTML>
<HEAD>
<TITLE>form</TITLE>


<SCRIPT language = "Javascript">



<!--

btnvalup = new Image; btnvalup.src = "images/BtnValidateGoldUp.jpg";
btnvaldown = new Image; btnvaldown.src = "images/BtnValidateGoldDown.jpg";

function MouseOver(BtnName)
{
      if (BtnName=="btnValidate")
      {
      document.btnValidate.src = btnvaldown.src;
      }
      
      if (BtnName=="btnSubmit")
      {
      document.btnSubmit.src = btnsubdown.src;
      }
}


function MouseOut(BtnName)
{
      if (BtnName=="btnValidate")
      {
      document.btnValidate.src = btnvalup.src;
      }
      
      if (BtnName=="btnSubmit")
      {
      document.btnSubmit.src = btnsubup.src;
      }
}


function check()
{
      var count=0;
      var msg="";

      for(i=0;i<=7;i++)
      {
               if(document.frmRegistration.elements[i].name=="regname")
            {
                   if(document.frmRegistration.elements[i].value.length<1)
                  {
                        count=1;
                        msg=msg+"\nName may not be left blank.";
                        document.frmRegistration.elements[i].focus();
                  }
                  
                  if(count==0)
                  {
                        if(document.frmRegistration.elements[i].value.length<6)
                        {
                              count=1;
                              msg=msg+"\nName must have a minimum of 6 characters.";
                              document.frmRegistration.elements[i].value="";
                              document.frmRegistration.elements[i].focus();
                        }
                  }
                  
                  if(document.frmRegistration.elements[i].value.length >25)
                  {
                        count=1;
                        msg=msg+"\nName cannot exceed 25 characters in length";
                        document.frmRegistration.elements[i].value="";
                        document.frmRegistration.elements[i].focus();
                  }
            }
            
            
            
            
            if(document.frmRegistration.elements[i].name=="email")
       {
                  emailchar=document.frmRegistration.email.value.split("@")

                  if(emailchar.length>1)
                  {
                        index=emailchar[1].indexOf(".")
                        if(index == -1)
                        {
                              document.frmRegistration.elements[i].focus()
                              return false
                        }
                        else
                        {
                              return true
                        }
                  }
                  else
                  {
                        document.frmRegistration.elements[i].focus()
                        return false
                  }
            }                  
 
            
            if(document.frmRegistration.elements[i].name=="login")
            {
                   if(document.frmRegistration.elements[i].value.length<1)
                  {
                        count=1;
                        msg=msg+"\nLogin Name may not be left blank.";
                        document.frmRegistration.elements[i].focus();
                  }
                  
                  if(count==0)
                  {
                        if(document.frmRegistration.elements[i].value.length<6)
                        {
                              count=1;
                              msg=msg+"\nLogin Name must have a minimum of 6 characters.";
                              document.frmRegistration.elements[i].value="";
                              document.frmRegistration.elements[i].focus();
                        }
                  }
                  
                  if(document.frmRegistration.elements[i].value.length >25)
                  {
                        count=1;
                        msg=msg+"\nLogin Name cannot exceed 25 characters in length";
                        document.frmRegistration.elements[i].value="";
                        document.frmRegistration.elements[i].focus();
                  }
            }      
            
            if(document.frmRegistration.elements[i].name=="password")
       {
                  if(document.frmRegistration.elements[i].value.length<1)
                  {
                        count=1;
                        msg=msg+"\nPassword may not be left blank.";
                        document.frmRegistration.elements[i].focus();
                  }
                  
                  if(count==0)
                  {
                        if(document.frmRegistration.elements[i].value.length<6)
                        {
                              count=1;
                              msg=msg+"\nPassword must have a minimum of 6 characters.";
                              document.frmRegistration.elements[i].value="";
                              document.frmRegistration.elements[i].focus();
                        }
                  }
                  
                  if(document.frmRegistration.elements[i].value.length >25)
                  {
                        count=1;
                        msg=msg+"\nPassword cannot exceed 25 characters in length";
                        document.frmRegistration.elements[i].value="";
                        document.frmRegistration.elements[i].focus();
                  }
            }
            
            else if((i>=7) && (count==0))
            {
                  return(true);
            }
      
      }
   
    for(i=0;i<=7;i++)
    {
        if(document.frmRegistration.elements[i].value=="")
        {
                  alert("PLEASE CORRECT THE FOLLOWING ERROR(S)\n "+msg);
                  document.frmRegistration.elements[i].focus();
                  return(false);
        }
    }
}
-->



</SCRIPT>
      
</HEAD>
 
<BODY ONLOAD=document.frmregistration.regname.focus() BGCOLOR = "#000066" TEXT = "#CC9900">

<CENTER>

<TABLE BORDER="1" CELLPADDING="0" WIDTH="55%">
      <TR>
            <TD>
                  <IMG SRC="images/HeadingRegistrationForm.jpg" NAME="heading" ALT="Registration Form" WIDTH="564" HEIGHT="63">

            </TD>
      </TR>
      
      <TR>
            <TD>
    <p align="center">
      <IMG SRC="images/LineGoldBlue.jpg" NAME="line" ALT="Gold Line" WIDTH="464" HEIGHT="6">

    </p>

            </TD>
      </TR>
</TABLE>
<center>
<FORM METHOD="post" ACTION="addDisplay.asp" NAME="frmRegistration">

<TABLE BORDER="1" CELLPADDING="3" WIDTH="427" height="279">
<center>
      <TR>
            <TD width="216" height="25"><font arial size="4">
               Name:
            </FONT></TD>

            <TD width="180" colspan="4" height="25">
                  <INPUT TYPE="text" NAME="regname" SIZE="35">
        </TD>

            <TD width="89" colspan="2" height="25">
        </TD>
      </TR>

    <tr>
            <TD width="216" height="25"><FONT Arial SIZE="4">
                  E-mail:
            </FONT></TD>
            
            <TD width="180" colspan="4" height="25">
                  <INPUT TYPE="text" NAME="email" SIZE="35">
            </TD>
            
            <TD width="89" colspan="2" height="25">
            </TD>
    </tr>
      <TR>
            <TD width="216" height="18"></TD>

            <TD width="269" colspan="6" height="18">
        </TD>
      </TR>

      <TR>
            <TD width="216" height="25"><font arial size="4">
                Login Name:
            </FONT></TD>

            <TD width="1" height="25">
                  <INPUT TYPE="text" NAME="login" SIZE="25">
        </TD>

            <TD width="0" colspan="2" height="25">
        </TD>

            <TD width="269" colspan="3" height="25">
        </TD>
      </TR>

      <TR>
            <TD width="216" height="41"><FONT Arial SIZE="4">
                  Password:
            </FONT></TD>

            <TD width="126" height="41">
                  <INPUT TYPE="password" NAME="password" SIZE="25">
            </TD>

 
            <TD width="111" rowspan="3" height="41" colspan="4">
            <p align="right" style="line-height: 100%"><font arial size="3"><b>
                              Remember&nbsp;&nbsp; password?</b></font>
                        <FONT Arial SIZE="4">
                    &nbsp;
                        </FONT>
            </TD>



            <TD width="32" rowspan="3" height="41">
            <INPUT TYPE="checkbox" NAME="checkbox" VALUE="yes" CHECKED>
            </TD>
      </TR>

    <tr>
            <TD width="216" height="18"></TD>

            <TD width="269" colspan="6" height="18"></TD>
    </tr>

    <tr>
            <TD width="216" height="23"><FONT Arial SIZE="4">
                  Title:
            </FONT></TD>

            <TD width="180" colspan="4" height="23"><FONT Arial SIZE="4">
                  <INPUT TYPE="radio" NAME="title" VALUE="Mr" CHECKED >Mr.  
                  <INPUT TYPE="radio" NAME="title" VALUE="Ms">Ms.  
                  <INPUT TYPE="radio" NAME="title" VALUE="Mrs">Mrs.  
            </FONT></TD>

            <TD width="89" colspan="2" height="23"></TD>
    </tr>

      <TR>
            <TD width="216" height="25"><FONT Arial SIZE="4">
                  Area of interest?
            </FONT></TD>
            
            <TD width="90" colspan="2" height="25">
             <SELECT SIZE="1" NAME="area">
                   <OPTION SELECTED VALUE="wd">Web Design</OPTION>
                   <OPTION VALUE="wp">Web Programming</OPTION>
                        <OPTION VALUE="wm">Web Marketing</OPTION>
                        <OPTION VALUE="wa">Web Administration</OPTION>
                  </SELECT>
            </TD>    
            
            <TD width="90" colspan="2" height="25">
            </TD>    
            
            <TD width="89" colspan="2" height="25">
            </TD>    
      </TR>

<!--      <TR HEIGHT="60" VALIGN="BOTTOM">-->
            
      
      
      
            <TD WIDTH="216" ALIGN="LEFT" height="18">
        <INPUT TYPE=image SRC="images/BtnValidateGoldUp.jpg" NAME="I10" ALT="Validate" ONCLICK="return check()" WIDTH="112" HEIGHT="23" onmouseover="MouseOver('btnvalidate')" onmouseout="MouseOut('btnvalidate')">
            </TD>
            
            <TD WIDTH="759" ALIGN="RIGHT" colspan="6" height="18">
        <INPUT TYPE=image SRC="images/BtnSubmitGoldUp.jpg" NAME="I11" ALT="Submit" " WIDTH="112" HEIGHT="23" onmouseover="MouseOver('btnsubmit')" onmouseout="MouseOut('btnsubmit')">
            </TD>
      
      
      </center>

</TABLE>

 
</FORM>
</center>

<font size = -1>All graphics used on this site were generated by using: <BR><a href="http://www.yahoo.com" style="color:
rgb(0,255,0)"><font color="FF00CC">http://www.flamingtext.com</font></a>
</BODY>
</HTML>
Avatar of seanpowell
seanpowell
Flag of Canada image

You have an extra quote:

ALT="Submit" " 
Now let me try and find the js problem...
SOLUTION
Avatar of seanpowell
seanpowell
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of needexpert
needexpert

ASKER

Let me try it.   I knew it was something so stupid... !!!! Thanks. Get right back to you.
Still not working - but not getting any errors...
It seems to be the Validate button on the left that isn't working and I took the ONCLICK out of the Submit button and that is where the extra " came from but if I put it back in then it still doesn't validate using the check() function.
I block out the email section which was the last thing I added and the validation works so it is in the email section of the check() function. I will keep looking.
Just doing the same thing...
Seems that I got this code to work except for I can put a @ and . together so I guess I would use the split here



if(document.frmRegistration.elements[i].name=="email")
         {
                  if(document.frmRegistration.elements[i].value.length>1)
                  {
                        if(document.frmRegistration.elements[i].value.indexOf('.') < 0 ||
                           document.frmRegistration.elements[i].value.indexOf('@') > 
                           document.frmRegistration.elements[i].value.indexOf('.'))
                        {
                              count=1;
                              msg=msg+"\nE-mail address is invalid.";
                              document.frmRegistration.elements[i].value="";
                              document.frmRegistration.elements[i].focus();
                        }
                  }
                  
                  
                  
                  if(count==0)
                  {
                        if(document.frmRegistration.elements[i].value.length<7)
                        {
                              count=1;
                              msg=msg+"\nA valid E-mail address must have a minimum of 7 characters.";
                              document.frmRegistration.elements[i].value="";
                              document.frmRegistration.elements[i].focus();
                        }
                  }
                  
                  if(document.frmRegistration.elements[i].value.length >25)
                  {
                        count=1;
                        msg=msg+"\nE-mail address cannot exceed 25 characters in length";
                        document.frmRegistration.elements[i].value="";
                        document.frmRegistration.elements[i].focus();
                  }
            }
How can I check to make sure > 1 between '@' and '.'?
Let me post a pointer to this Q from the Javascript section...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
or
...


regEx= /^\w+([\.-]?\w+)*@\w+([\.-]?)\w*(\.\w{2,4})+$/


and in your validation routine

if(!regEx.test(textboxEmail.value)){
   alert("Invalid email format. Please enter a valid email.\n");
   return false;
}
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
function checkEmail(myForm) {
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm.emailAddr.value)){
return (true)
}

PS. A ".info" address would fail this validation!

BFN,

fp.
Suggest this instead:

function checkEmail(myForm)
{
  return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(myForm.emailAddr.value));
}
not mine
i would be glad to redo this whole form validation for you, but i need to know why you
are doing all of this looping?

I really do not see any need for a loop, unless all fields are required, but if they are
then we can make this code shorter, neater, easier to maintain.

Please let me know what exactly you wanna do
already done Jay

      function check(){
    var count=0;
    var msg="";
      var strErr="";
      regExName=/^[A-Z][A-Z '-]{4,23}[A-Z]$/gi;
      regExLog=/^[A-Z][A-Z '-]{4,23}[A-Z]$/gi;
      regExPass=/^[A-Z][A-Z '-]{4,23}[A-Z]$/gi;
      regExEmail= /^\w+([\.-]?\w+)*@\w+([\.-]?)\w*(\.\w{2,4})+$/;
      if(!regExName.test(document.getElementById('regname').value)){
            if(!strErr){
                  strErr='regname';
            }
            
            msg=msg+"\nInvalid format for Name.\n Name must be between 6 and 25 characters in length.";
      }      
      if(!regExEmail.test(document.getElementById('email').value)){
            if(!strErr){
                  strErr='email';
            }
            
            msg=msg+"\nInvalid format for Email.\n Please enter a valid email";
      }      
            
      if(!regExLog.test(document.getElementById('login').value)){
            if(!strErr){
                  strErr='login';
            }
            
            msg=msg+"\nInvalid format for Log In ID.\n Log In ID must be between 6 and 25 characters in length.";
      }      
      
      if(!regExPass.test(document.getElementById('password').value)){
            if(!strErr){
                  strErr='password';
            }
            msg=msg+"\nInvalid format for Password.\n Password must be between 6 and 25 characters in length.";
      }      
      
      if(msg){
            alert(msg);
            document.getElementById(strErr).focus();
      }      
      return (msg ? false : true);
}
-->
Not really jester

yours will only work in browsers that support document.getElementById

you should use document.forms[0]

also to put focus to the first element, i would (since its a textbox) use document.forms[0].elements[0].focus();

also use <form name="form1" .......... onsubmit="return check();">


and do NOT use <input type="button"...onclick="check();">

use <input type="submit" name="btnSubmit" value="Submit">
>>yours will only work in browsers that support document.getElementById
oops, thought this was ns 4+ but it is 6... my fault

>>also to put focus to the first element, i would (since its a textbox) use document.forms[0].elements[0].focus();
should be part of the onload, not the check function

>>also use <form name="form1" .......... onsubmit="return check();">
>><input type="submit" name="btnSubmit" value="Submit">
always do

>>and do NOT use <input type="button"...onclick="check();">
never

;-)
ok here it is ns4+ and ie5+ compatible

      <script>
      function check(){
    var count=0;
    var msg="";
      var strErr="";
      regExName=/^[A-Z][A-Z '-]{4,23}[A-Z]$/gi;
      regExLog=/^[A-Z][A-Z '-]{4,23}[A-Z]$/gi;
      regExPass=/^[A-Z][A-Z '-]{4,23}[A-Z]$/gi;
      regExEmail= /^\w+([\.-]?\w+)*@\w+([\.-]?)\w*(\.\w{2,4})+$/;
      if(!regExName.test(document.forms[0].elements['regname'].value)){
            if(!strErr){
                  strErr='regname';
            }
            
            msg=msg+"\nInvalid format for Name.\n Name must be between 6 and 25 characters in length.";
      }      
      if(!regExEmail.test(document.forms[0].elements['email'].value)){
            if(!strErr){
                  strErr='email';
            }
            
            msg=msg+"\nInvalid format for Email.\n Please enter a valid email";
      }      
            
      if(!regExLog.test(document.forms[0].elements['login'].value)){
            if(!strErr){
                  strErr='login';
            }
            
            msg=msg+"\nInvalid format for Log In ID.\n Log In ID must be between 6 and 25 characters in length.";
      }      
      
      if(!regExPass.test(document.forms[0].elements['password'].value)){
            if(!strErr){
                  strErr='password';
            }
            msg=msg+"\nInvalid format for Password.\n Password must be between 6 and 25 characters in length.";
      }      
      
      if(msg){
            alert(msg);
            document.forms[0].elements[strErr].focus();
      }      
      return (msg ? false : true);
}
-->
</script>
my version of just email validation

<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<!--
function isEmail(strValue){
      var objRegExp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
      return objRegExp.test(strValue);
}
function validateForm(fObj){
      var strErr = "";
      var focusField = "";
      if(!isEmail(fObj.email.value)){
            strErr += "Email is in the wrong format\n";
            if(focusField == "") focusField = "email";
      }
      if(strErr != ""){
            alert("The following fields contain errors\n\n"+ strErr);
                  eval("fObj."+ focusField +".focus()");
                        return false;
      }
   return true;
}
// -->
</script>
</head>
<body>
<form name="form1" method="post" action="" onsubmit="return validateForm(this);">
      <input type="text" name="email">
      <input type="submit" name="btnSubmit" value="Submit">
</form>
</body>
</html>
NO POINTS TO ME
>>NO POINTS TO ME
??

why would you say that?


The asker is going to have a hard time piecing all this together... any way we can agree on which is the best solution?
No need in them, i was just posting another way to do it, so that if anyone else searches and
finds this once its paq they will have alternitives.

let jay decide
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i added a submit button for testing purposes.

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I managed to get it done without reading these posted comments, however l appreciate everyones comments and help. I will review and decide where to distribute the points and I know that I will learn from your postings.
Thanks again.
np... glad i was able to help
There is no known email regex that is perfect, there are dozens and dozens of valid email forms this is one ot he better regex's

/(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/