Problem with javascript

in the validate() function itself i have been checking all the conditions…

k i have started working with that previous code but it is also not executing…

Do you have a test page for what you currently have? If so, link to it. If not, put it up on jsfiddle.net and link us to that test page.

k below is my link to the script…http://jsfiddle.net/ravi951/NfjwK/

Thanks. You’ve messed up the validate function and the way it relates to the rest of the script.

Go back to the script that you first began with. If you have any confusion about which script that is, it is the script that you posted in the original post of this thread.

k there is another successnew.html which i have not posted.
i am looking at my script tell me what went wrong…

Which you have not posted?

Let me know when you have decided to do so, and the link for the latest version. :slight_smile:

no it is simple just displays success message below is the one…
i dont know where to paste there in jsfiddle…

successnew.html


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Dynamic Inline Form Validation Demo</title>
<link rel="stylesheet" type="text/css" href="messagesnew.css" />
<script type="text/javascript" src="messagesnew.js"></script>
</head>
<body>
<div id="wrapper">
  Successfully submitted!
</div>
</body>
</html>

I am still seeing at the link you provided, what looks to still be the broken code you’re having troubles with. It is not the script that you first began with.

Have you replaced the script with the original script that you first began with? If so, and if that’s what you’re still having trouble with, a link to that version would be appreciated.

are u unable to see my fiddle below is the one

http://jsfiddle.net/ravi951/Rp29D/1/

is there any thing extra to select in jsfiddle like framework or any other things…

i have copied each script in to its own block and clicked the save button…
and the link is given to u…

this is the original script i am working before

Yes, I can see that there. The script in that page starts with:


// form validation function //

function validate() 
{
   function checkName(form) 

That is the non-functioning script.

The script that I expect to see is that which you used on your first post to this thread.

do u mean this one…


// form validation function //

function validate() 
{
    var form = document.forms['form'];
    var ary = [checkName,checkEmail,validatePwd,validPhone];
    var rtn = true;
    var z0 = 0;
    for (var z0 = 0; z0 < ary.length; z0++) 
    {
        if (!ary[z0](form)) 
        {
            rtn = false;
        }
    }
    return rtn;
}
 
function checkName(form) 
{
    var eobj = document.getElementById('realnameerror');
    var sRealName = form.realname.value;
    var oRE = /^[a-z0-9]+[_.-]?[a-z0-9]+$/i;
    var error = false;
    eobj.innerHTML = '';
    if (sRealName == '') 
     {
        error = 'Error: Username cannot be blank!';
     }
    else if (sRealName.length < 4) 
    {
        error = "UserName should be atleast 4 characters long";
    }
    else if (!oRE.test(sRealName)) 
    {
        error = "Incorrect format.";
    }
    if (error) 
    {
        if (hasFocus == false) 
         {
            form.realname.focus();
            hasFocus = true;
         }
        eobj.innerHTML = error;
        return false;
    }
    return true;
}
 
function checkEmail(form)          /* for email validation */ 
{
    var eobj = document.getElementById('emailerror');
    eobj.innerHTML = '';
    var error = false;
    if(form.email.value.length == 0) 
    {
        error = 'Please enter email.';
    } else if (/^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})+$/.test(form.email.value)) 
       {
        return true;
       } else 
          {
        error = 'Invalid E-mail Address! Please re-enter.';
          }
    if (error) 
     {
        eobj.innerHTML = error;
        if (!hasFocus) 
          {
            form.email.focus();
            hasFocus = true;
          }
        return false;
     }
     return true;
}

function validatePwd(form)          /* password & retype-password verification */ 
{
    var eobj1 = document.getElementById('passworderror');
    var eobj2 = document.getElementById('password2error');
    var minLength = 6;
    var invalid = ' ';
    var pw1 = form.password.value;
    var pw2 = form.password2.value;
    var error = false;
    eobj1.innerHTML = '';
    eobj2.innerHTML = '';
    if (pw1.length < 1) 
	{
        error = 'Please enter your password.';
    }

    else if (pw1.length < minLength) 
	{
        error = 'Your password must be at least ' + minLength + ' characters long. Try again.';
    }

    else if (pw1.indexOf(invalid) > -1) 
	{
        error = 'Sorry, spaces are not allowed.';
    }

    else if (pw2.length == 0) 
	{
        error = 'Please retype password.';
        if (!hasFocus)
		{
            form.password2.focus();
            hasFocus = true;
        }
        eobj2.innerHTML = error;
        return false;
    }
    if (error) 
	{
        if (!hasFocus) 
		{
            form.password.focus();
            hasFocus = true;
        }
        eobj1.innerHTML = error;
        return false;
    }

    if (pw1 != pw2) 
	{
        eobj2.innerHTML = ' passwords not matching.Please re-enter your password.';
        if (!hasFocus) 
		{
            form.password2.focus();
            hasFocus = true;
        }
        return false;
    }
    return true;
}

function validPhone(form)              /* phone no validation */ 
{
    var eobj = document.getElementById('phonenoerror');
    var valid = '0123456789';
    var phone = form.phoneno.value;
    var error = false;
    var i = 0;
    var temp;
    eobj.innerHTML = '';
    if (phone == '') 
	{
        error = 'This field is required. Please enter phone number';
    }
    else if (!phone.length > 1 || phone.length < 10) 
	{
        error = 'Invalid phone number length! Please try again.';
    }
    else 
	{
        for (i = 0; i < phone.length; i++) 
		{
            temp = '' + phone.substring(i, i + 1);
            if (valid.indexOf(temp) == -1) 
			{
                error = 'Invalid characters in your phone. Please try again.';
            }
        }
    }
    if (error) 
	{
        if (!hasFocus) 
		{
            form.phoneno.focus();
            hasFocus = true;
        }
        eobj.innerHTML = error;
        return false;
    }
    return true;
}
// START OF MESSAGE SCRIPT //

var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 3;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
  var msg;
  var msgcontent;
  if(!document.getElementById('msg')) {
    msg = document.createElement('div');
    msg.id = 'msg';
    msgcontent = document.createElement('div');
    msgcontent.id = 'msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('msg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);
  targetdiv.focus();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + 'px';
  msg.style.left = leftposition + 'px';
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;  
  }
  window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('msg');
  if(!msg.timer) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}

// face the message box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}
// preload the arrow //
if(document.images) {
  arrow = new Image(7,80); 
  arrow.src = "images/msg_arrow.gif"; 
}

No, I mean the one from your first post to this thread.

Do you know how to find the first post?

this is the first script i have posted i have taken it from where i posted…


// form validation function //
 function checkName(form)
{
  var eobj=document.getElementById('realnameerror');
  var sRealName = form.realname.value;
  var oRE = /^[a-z0-9]+[_.-]?[a-z0-9]+$/i;
  var error=false;
  eobj.innerHTML='';
  if (sRealName == '') 
  {
   error='Error: Username cannot be blank!';
  }
  else if (sRealName.length < 4) 
  {
    error="UserName should be atleast 4 characters long";
  }
  else if (!oRE.test(sRealName))
  {
   error="Incorrect format.";
  }
  if (error)
{
   if (hasFocus == false) 
   {
     form.realname.focus();
     hasFocus = true;
   } 
   eobj.innerHTML=error;
   return false;
  }
  return true;
 }

function checkEmail(form)          /* for email validation */
{
 var eobj=document.getElementById('emailerror');
 eobj.innerHTML='';
 var error = false;
  if (form.email.value.length == 0) 
  {
    error = 'Please enter email.';
  } else if (/^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})+$/.test(form.email.value))
 {
  return true;
 } else 
 {
   error = 'Invalid E-mail Address! Please re-enter.';
 }
 if (error) 
{
   eobj.innerHTML=error;
   if (!hasFocus) 
   {
     form.email.focus();
     hasFocus = true;
   } 
   return false;
 }

 return true;
}

function validatePwd(form)          /* password & retype-password verification */
{       
 var eobj1=document.getElementById('passworderror');
 var eobj2=document.getElementById('password2error');
 var minLength=6;
 var invalid=' ';
 var pw1=form.password.value;
 var pw2=form.password2.value;
 var error=false;
 eobj1.innerHTML='';
 eobj2.innerHTML='';
 if (pw1.length<1)
 {
  error='Please enter your password.';
 }
 else if (pw1.length < minLength)
 {
  error='Your password must be at least ' + minLength + ' characters long. Try again.';
 }
 else if (pw1.indexOf(invalid) > -1)
 {
  error='Sorry, spaces are not allowed.';
 } 
else if (pw2.length == 0) 
{
  error='Please retype password.';
   if (!hasFocus) 
   {
     form.password2.focus();
     hasFocus = true;
   } 
  eobj2.innerHTML=error;
  return false;
 }
 if (error)
 {
   if (!hasFocus) 
   {
     form.password.focus();
     hasFocus = true;
   } 
    eobj1.innerHTML=error;
  return false;
 }
 if (pw1 != pw2)
 {
  eobj2.innerHTML=' passwords not matching.Please re-enter your password.';
   if (!hasFocus) 
   {
     form.password2.focus();
     hasFocus = true;
   } 
  return false;
 }
 return true;
}

function validPhone(form)              /* phone no validation */
{          
 var eobj=document.getElementById('phonenoerror');
 var valid = '0123456789';
 var phone = form.phoneno.value;
 var error=false;
 var i=0;
 var temp;
 eobj.innerHTML='';
 if (phone == '')
 {
  error='This field is required. Please enter phone number';
 }
 else if (!phone.length > 1 || phone.length < 10)
 {
  error='Invalid phone number length! Please try again.';
 }
 else 
 {
  for (i=0; i < phone.length; i++)
 {
   temp = '' + phone.substring(i, i + 1);
   if (valid.indexOf(temp) == -1)
    {
    error='Invalid characters in your phone. Please try again.';
    }
  }
 }
 if (error)
 {
   if (!hasFocus) 
   {
     form.phoneno.focus();
     hasFocus = true;
   } 
  eobj.innerHTML=error;
  return false;
 }
 return true;
}

function validate() 
 {
  hasFocus = false;
 var form = document.forms['form'];
 var ary=[checkName,checkEmail,validatePwd,validPhone];
 var rtn=true;
 var z0=0;
 for (var z0=0;z0<ary.length;z0++)
{
  if (!ary[z0](form))
  {
    rtn=false;
  }
 }
 return rtn;
}

// START OF MESSAGE SCRIPT //

var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 3;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
  var msg;
  var msgcontent;
  if(!document.getElementById('msg')) {
    msg = document.createElement('div');
    msg.id = 'msg';
    msgcontent = document.createElement('div');
    msgcontent.id = 'msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('msg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);
  targetdiv.focus();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + 'px';
  msg.style.left = leftposition + 'px';
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;  
  }
  window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('msg');
  if(!msg.timer) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}

// face the message box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}
// preload the arrow //
if(document.images) {
  arrow = new Image(7,80); 
  arrow.src = "images/msg_arrow.gif"; 
}

Yes, that is the one that I mean.

in login.html i have the below form

<form name=“form” id=“form” class=“form” action=“successnew.html” onsubmit=“return validate(this)” method=“post”>

means on submitting the validate() button i want javascript function to execute
which is i just now given to you…

with out clicking how can we check the validate() for all the functions like
username,email id…etc…

What do you mean by “without clicking”? By blinking our eyes rapidly instead? Or by using mind power in some way?

when we click on the “submit” button only it will check the validate() function
right…
now i need to seperately work for validate() function…
what i did is in validate() function i am checking each function individually which is username() function,emailid() function,password() function and so on …
is that the wrong method what i am doing…

Not just only that. The validate function checks the username, email, password, and phone.

You don’t need to edit the script at all in any way.

then how to assign validate() function to the javascript which i have created in my HTML code…

then is there any other way to do like that means with out assigning validate() function to javscript…