2014-04-19 64 views
0

我的HTML代碼如下js和jQuery驗證工作不

<script src="js/validate.js"></script> 
<label>FIRST NAME:</label> 
</td> 
<td> 
<input type="text" class="firstname" id="firstname" onKeyUp="firstname()" /> 
</td> 
<td> 
<label id="fn"></label> 

和我的JS代碼如下

function firstname() 
{ 
    var nname=document.getElementById("firstname").value; 
    var l=nname.length > 3 ; 
    if(!l) 
    { 
    producePrompt("firstname should not be less than 4 characters","fn","red");  
    return false; 
    } 
    else 
    if(!nname.match(/^[a-zA-Z\s]*$/)) 
    { 
     producePrompt("incorrect name","fn","red"); 
     return false; 
    } 
    else 
    { 
    producePrompt("correct","fn","green"); 
    return true; 
    } 
} 

function producePrompt(message,promptlocation,color) 
{ 
    document.getElementById(promptlocation).innerHTML= message; 
    document.getElementById(promptlocation).style.color= color;  
} 

,但我的代碼沒有顯示消息或不會驗證它正確 的js在一個名爲validate.js的文件中,html位於index.html中 我試過兩種類型(,)以包含js文件但未驗證正在發生

+0

你能把這一個js小提琴?包括也validate.js – JohanVdR

+0

對不起,我沒有聽說關於js小提琴 – user3447573

+0

這裏是鏈接http://jsfiddle.net/makk/6d6W4/ – user3447573

回答

0

try thi S,工作正常,我(驗證工作):

JS:

function checkinput() 

{ 

    var nname=document.getElementById("firstname").value; 
    var l=nname.length > 3 ; 
if(!l) 
{ 
console.log("firstname should not be less than 4 characters","fn","red");  
return false; 
} 
else 
if(!nname.match(/^[a-zA-Z\s]*$/)) 
{ 
    console.log("incorrect name","fn","red"); 
    return false; 
} 
else 
{ 
    console.log("correct","fn","green"); 
return true; 
    } 
} 

HTML:

<form> 

<label>FIRST NAME:</label> <input type="text" class="firstname" id="firstname" onblur="checkinput()" /> 

</form> 
0
<body> 
    // all your html contents 

    // load the scripts here 
<script src="js/validate.js"></script> 
</body> 

OR

<body> 
    // all your html contents 

    <script> 
    // javascript contents 
    </script> 

</body>