0

I have a strange problem with validating an html form with javascript. I'm very new to this but i programmed with other languages already.

So when you click on the "Absenden" button js is supposed to check if all required fields are filled out, so far it should only give a warning if a requiered field is left blank. So in this simple function there's something wrong that can't find: (that's the senden.js)

function initCheck()
{
var error = new String;
var N = document.getElementById("Name").value;
var E = document.getElementById("Email").value;
var F = document.getElementById("Feedback").value;

if( N.length == 0 ){ error += 'Bitte geben Sie ihren Nachnamen ein!\n'; } 

if( E.length == 0 ){ error += 'Bitte geben Sie ihre E-Mail ein!\n'; }

if( F.length == 0 ){ error += 'Bitte geben Sie ihr Feedback ein!\n'; }

alert(error);

return true;
}

somehow the Part with the Email is never beeing shown in the alarm box. I have no clue why... I checked like a thousand times for a typo. Also the chrome console doesn't recognize any errors. I don't get why it never gets the email notice, the rest works.

I can imagine that the problem might be in an other js function also beeing used at this page, because it also grabs the email textfield, but it saves the value in a different variable and doesn't change the original, so there shouldn't be a problem right?: (validate.js)

function ValidateEmail()
{       
    var x = new String;
    x = document.getElementById("Email").value;

    if ( CheckSyntax(x) == false ) { EmailAlert(); }
    return true;

}

function EmailAlert()
{
alert('falsche E-Mail Syntax! (at least like:[email protected])');
}

function CheckSyntax(x)
{   
var at = x.indexOf("@");
var dot = x.indexOf(".");
var str3 = (x.length - dot);

if ( x.length == 0 ) {return false;} 

if( x.length > 255 ) {return false;}

if( at <= 1 || at != x.lastIndexOf("@") ) { return false; } 

if( dot <= ( at + 1 ) ) { return false; }

if( str3 < 2 || str3 > 4 ) { return false } 

return true;
}

and tooltip.js:

function initTooltips()
{

var infodiv = document.createElement('DIV');
infodiv.id = 'info';
document.getElementsByTagName('body')[0].appendChild(infodiv);


var liste = document.getElementsByTagName('A'); 

for (var n=0; n<liste.length; n++) 
{
    if(liste[n].className.match( /tooltip/ ))
    {
        liste[n].onmousemove = showTip;
        liste[n].onmouseout =  hideTip;
    }
}


}
function showTip(ev)
{

if(!ev) { ev = window.event; }

var infotext = '';

for(var n=0; n<this.childNodes.length; n++)
{
    if(this.childNodes[n].tagName == "SPAN")
    {
        infotext = this.childNodes[n].innerHTML; break;
    }
} 

if(infotext.length)
{
    infodiv = document.getElementById('info');
    infodiv.innerHTML = infotext;

    infodiv.style.display = 'block';

    var scrollOffset = getScrollOffset();

    infodiv.style.top  = ev.clientY + scrollOffset[1] + 20 + 'px';
    infodiv.style.left  = ev.clientX + scrollOffset[0] + 20 + 'px';
}
}

function hideTip()
{ 
 document.getElementById('info').style.display = 'none'; 
}

function getScrollOffset()
{
  var x = 0, y = 0;
  if (self.pageYOffset)          // all except Explorer
  {
      x = self.pageXOffset;
      y = self.pageYOffset;
  }
  else if (document.documentElement && document.documentElement.scrollTop)
           // Explorer 6 Strict
  {
      x = document.documentElement.scrollLeft;
      y = document.documentElement.scrollTop;
  }
  else if (document.body)           // all other Explorers
  {
      x = document.body.scrollLeft;
      y = document.body.scrollTop;
  }
  return new Array(x,y);
}

I fear there is a problem with the memory somehow? Unfortunately that's way out of my experiences with js so far... so please if you have an Idea to solve the problem, let me know.

I attach the rest of the sources in pastebin, so this post doesn't get too big.

html: http://pastebin.com/pjh0vdEJ css: http://pastebin.com/nZaVLFCs (a little messy but it works)

Any help is much needed, cuz I don't even know how to name the problem to find an answer via google...

1 Answer 1

2

In the HTML you linked to, the default value has a nonzero length:

<input id="Email" type="text" value=" " seize="40" onblur="ValidateEmail()">

...but your JS is comparing to zero:

if( E.length == 0 ){ error += 'Bitte geben Sie ihre E-Mail ein!\n'; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.