电话号码未通过javascript验证

时间:2013-11-23 21:43:12

标签: javascript forms

我正在制作表单并且我的验证功能有效,但完全忽略了电话号码并允许发送。

我需要做的是,如果电话号码不包含10个号码,则会被拒绝。

如果我复制确切的代码并将其放在一个单独的函数中:

<button onclick="CheckTelephone()">Click</button>


function CheckTelephone(){
var Telephone       = document.forms["OrderForm"]["Telefoon"]       .value.length;

if (Telefoon != 10){
    document.getElementById("Error").innerHTML = "Telefoon nummer moet 10 cijfers bevatten.";
    setTimeout("ErrorClear()",5000);
    return false;}
}

但是当我在完整代码上提交表单时它允许发送任何数字:(一直在底部)

<form onsubmit="return validate()" name="OrderForm">
<input type="submit" id="SubmitForm" name="SubmitForm" value="Plaats Bestelling">

// Form validation
function validate(){
// Variables
var TotalAmount     = document.getElementById('TotalAmount').innerHTML;

var Voornaam        = document.forms["OrderForm"]["Voornaam"]       .value;
var Achternaam      = document.forms["OrderForm"]["Achternaam"]     .value;
var Tussenvoegsel   = document.forms["OrderForm"]["Tussenvoegsel"]  .value;
var Email           = document.forms["OrderForm"]["Email"]          .value;
var EmailCheck      = document.forms["OrderForm"]["EmailCheck"]     .value;
var Telefoon        = document.forms["OrderForm"]["Telefoon"]       .value.length;

var PositionAt      = document.forms["OrderForm"]["Email"]          .value.indexOf('@');
var PositionDot     = document.forms["OrderForm"]["Email"]          .value.indexOf('.');

var NotNumbers          = /^[0-9]$/;

// Checks filled out or not ( || = OR(= Of))
if  (TotalAmount == 0 || TotalAmount <= 0){
    document.getElementById("Error").innerHTML = "U heeft geen hoeveelheid oliebollen gekozen.";
    setTimeout("ErrorClear()",5000);
    return false;}
if  (  Voornaam   == null  || Voornaam   == "" 
    || Achternaam == null  || Achternaam == "" 
    || Email      == null  || Email      == "" 
    || EmailCheck == null  || EmailCheck == "" 
    || Telefoon   == null  || Telefoon   == ""){
    document.getElementById("Error").innerHTML = "Niet alle verplichte velden zijn ingevuld.";
    setTimeout("ErrorClear()",5000);
    return false;}

// Checks if email adresses are the same or contains . and @
if  (PositionAt <1 || PositionDot-PositionAt <2 ){
    document.getElementById("Error").innerHTML = "Het E-Mail adres bevat geen @ of . tekens.";
    setTimeout("ErrorClear()",5000);
    return false;}
if  (EmailCheck == Email ){return true;}else{
    document.getElementById("Error").innerHTML = "De ingevoerde E-Mail adressen zijn niet gelijk.";
    setTimeout("ErrorClear()",5000);
    return false;}

// Checks if Telephone number contains 10 digits
if (Telefoon != 10){
    document.getElementById("Error").innerHTML = "Telefoon nummer moet 10 cijfers bevatten.";
    setTimeout("ErrorClear()",5000);
    return false;}
// Somehow lets the phone number go trough even tough it's the exact same code


// If all correct
return true;
}

如果有人能告诉我为什么会这样,或者我做错了,我们将不胜感激

1 个答案:

答案 0 :(得分:1)

你的函数调用,

onclick="CheckTelefoon()" ....

和你的功能定义,

function CheckTelephone(){ ....

使两者都是唯一的,无错误地运行代码。