2016-06-11 51 views
0

有沒有一種更簡單的方法來測試角色,我在前一天看到有關RegExp的一些信息,但是它的應用有所不同,那麼我現在正在做什麼。該PIONT是例如:JavaScript - 過濾器,RegExp和變量的問題

如果姓名中只包含a to z那麼返回true如果給定數+1,後來一個當計數爲8或更高,將提交後。

但它不工作,當我putin號在名字。

做一些你發現錯誤的腳本或我可以做得更好?

非常感謝。

var count = 0; 
 

 
function checkEmail() { 
 

 
    var email = document.getElementById('Email'); // haalt record op en slaat op als variable 
 
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})/; // hier wordt gecheckt of de records correct zijn ingevuld 
 

 
    // hier staat als de test terug komt moet dingen die niet in de filter staat dan voer iets geldig in 
 
    if (!filter.test(email.value)) { 
 
    alert('Voer geldig email adres in.'); 
 
    email.focus;// hier wordt gelet als de muis of met die toetsenbord weg gaat van veld 
 

 
      return false; 
 
    
 

 
} 
 
else { 
 
    count = count+1; 
 
} 
 
    
 
} 
 

 
function checkVoornaam() { 
 

 
    var voornaam = document.getElementById('voornaam'); 
 
    var filter = /^([a-zA-Z])/; 
 

 
    if (!filter.test(voornaam.value)) { 
 
    alert('Voer uw voornaam in.'); 
 
    voornaam.focus; 
 
    return false; 
 
} 
 
else { 
 
    count = count+1; 
 
} 
 
} 
 

 
function checkachternaam() { 
 

 
    var achternaam = document.getElementById('achternaam'); 
 
    var filter = /^([a-zA-Z])/; 
 

 
    if (!filter.test(achternaam.value)) { 
 
    alert('Voer uw achternaam in.'); 
 
    achternaam.focus; 
 
    return false; 
 
} 
 
else { 
 
    count = count+1; 
 
} 
 
} 
 

 

 

 
function checkStraat() { 
 

 
    var straat = document.getElementById('Straatnaam'); 
 
    var filter = /^([a-zA-Z0-9,#.-]+)/; 
 

 
    if (!filter.test(straat.value)) { 
 
    alert('Voer uw straatnaam in.'); 
 
    straat.focus; 
 
    return false; 
 
} 
 
else { 
 
    count = count+1; 
 
} 
 
} 
 

 
function checkSn() { 
 

 
    var sn = document.getElementById('Huisnummer'); 
 
    var filter = /^([a-zA-Z0-9,#.-]+)/; 
 

 
    if (!filter.test(sn.value)) { 
 
    alert('Voer uw huisnummer in.'); 
 
    sn.focus; 
 
    return false; 
 
} 
 
else { 
 
    count = count+1; 
 
} 
 
} 
 

 
function checkPS() { 
 

 
    var ps = document.getElementById('Postcode'); 
 
    var filter = /^([1-9][0-9]{3}\s?[a-zA-Z]{2})/; 
 

 
    if (!filter.test(ps.value)) { 
 
    alert('Voer uw postcode in.'); 
 
    ps.focus; 
 
    return false; 
 
} 
 
else { 
 
    count = count+1; 
 
} 
 
} 
 

 
function checkWP() { 
 

 
    var WP = document.getElementById('Woonplaats'); 
 
    var filter = /^([a-zA-Z\-']+)/; 
 

 
    if (!filter.test(WP.value)) { 
 
    alert('Voer uw woonplaats in.'); 
 
    WP.focus; 
 
    return false; 
 
} 
 
else { 
 
    count = count+1; 
 
} 
 
} 
 

 
function checkTel() { 
 

 
    var tel = document.getElementById('Telefoonnummer'); 
 
    var filter = /^(\d{3}\d{3}\d{4})/; 
 

 
    if (!filter.test(tel.value)) { 
 
    alert('Voer uw telefoonnummer in.'); 
 
    tel.focus; 
 
    return false; 
 
} 
 
else { 
 
    count = count+1; 
 
} 
 
} 
 

 

 

 
// global var 
 
    var pass1 = document.getElementById('Wachtwoord'); 
 
var pass2 = document.getElementById('Herhaal_Wachtwoord'); 
 

 

 

 
// functie checkt terplekke of ww1 en ww2 overeenkomen 
 
function checkPass() 
 
{ 
 
    //Store the Confimation Message Object ... 
 
    var message = document.getElementById('confirmMessage'); 
 
    //Set the colors we will be using ... 
 
    var goodColor = "#66cc66"; 
 
    var badColor = "#ff6666"; 
 
    //Compare the values in the password field 
 
    //and the confirmation field 
 
    if(pass1.value == pass2.value){ 
 
     //The passwords match. 
 
     //Set the color to the good color and inform 
 
     //the user that they have entered the correct password 
 
     pass2.style.backgroundColor = goodColor; 
 
     message.style.color = goodColor; 
 
     message.innerHTML = "Passwords Match!"; 
 
//  registreren.register show(); 
 
    }else{ 
 
     //The passwords do not match. 
 
     //Set the color to the bad color and 
 
     //notify the user. 
 
     pass2.style.backgroundColor = badColor; 
 
     message.style.color = badColor; 
 
     message.innerHTML = "Passwords Do Not Match!"; 
 
    //  registreren.register hide(); 
 
    } 
 
} 
 

 

 
function validateForm() { 
 
    
 
    
 
var fields = ["voornaam", "achternaam", "Email", "Wachtwoord", "Herhaal_Wachtwoord", "Straatnaam", "Huisnummer", "Postcode","Woonplaats","Telefoonummer"]; 
 
     
 
if (pass1.value !== pass2.value){ 
 
     alert ("Wachtwoord komen niet overeen"); 
 
     return false; 
 
     } 
 
           if (count < 8){ 
 
         alert("iets is niet goed ingevuld"); 
 
         return false; 
 
         } 
 
      var l = fields.length; 
 
      var fieldname; 
 
       for (i = 0; i < l; i++) { 
 
       fieldname = fields[i]; 
 
        if (document.forms["register"][fieldname].value === "") { 
 
        alert(fieldname + " mag niet leeg zijn"); 
 
        return false; 
 
        } 
 
       } 
 
        // if (count < 8){ 
 
         // alert("iets is niet goed ingevuld"); 
 
         // return false; 
 
         // } 
 
       
 
        
 
        
 
       
 
      }

+0

請創建的jsfiddle或plunker。一個工作代碼有助於解決問題 – brk

+0

您的描述不太清楚。你能否正確解釋你想達到的目標?可能給出一些輸出示例。 – Rajesh

+0

你在哪裏聲明'count',你在哪裏檢查它的值,你在哪裏調用這些函數,以及你在哪裏提交表單? – trincot

回答

0

正則表達式/^([a-zA-Z])/只匹配字符串是否與AZ開頭,試試這個正則表達式:

/^[a-zA-Z]+$/ 

和:

function valid_firstname(firstName) { 
    return /^[a-zA-Z]+$/.test(firstName); 
} 

valid_firstname('John'); // true 
valid_firstname('John42'); // false 
0

在功能checkVoornaam的正則表達式:

var filter =/^([a-zA-Z])/;

小姐$炭末。

現在它會匹配馬里奧,也馬里奧1和Mario2Ciao等。

它應該是:

var filter = /^([a-zA-Z])$/;