2013-11-23 1927 views
0

我正在做的形式和我的驗證功能的工作原理,但完全忽略的電話號碼,並允許它被髮送。電話號碼沒有得到驗證的JavaScript

我需要發生的是,如果電話號碼犯規正好包含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

你的函數調用,

onclick="CheckTelefoon()" .... 

和你的函數定義,

function CheckTelephone(){ .... 

使兩者特有的無差錯運行代碼。

+0

對不起,那是真的很快添加按鈕時一個錯字,第一代碼的工作,當我按一下按鈕,但是當我真正嘗試同樣的事情,但我的完整形式就可以忽略它,簡單的形式發送關閉(代碼的第二個框) – Paradoxis

+0

@Paradoxis是控制檯抱怨任何錯誤? –

+0

控制檯不給任何錯誤,當我按下提交它就像我在正確填寫,並把它發送,在那裏,如果我搞砸任何其他領域,它會給我,告訴我要糾正一個錯誤領域。 – Paradoxis