Page 1 of 1

checking textbox field

Posted: Tue May 26, 2009 5:07 am
by rrn
in my website ther is a textbox for entering year..

i have given code for displaying an alert message if the textbox is left empty..

Code: Select all

function validate() {
if(document.form.year.value=="")
{
alert ('Please enter year');
return false;
}}


it is working , but i want to do is..

it should display an alert message if the length of the numbers is less than 4 or greater than 4

how can i do that??

please give me a solution...
thanks......

Re: checking textbox field

Posted: Tue May 26, 2009 8:41 am
by oliur
try this:

Code: Select all

function validate(form1){
 
    var userValue = form1.year.value;
    
    if(isNaN(userValue)){
        alert("This is not a number. Please try again");
        //reset the field
        form1.year.value="";
        return false;
    }
 
    var len = userValue.length; 
    if(len<4) alert("less than four");
    if(len>4) alert("more than four");
    if(len==4) alert("exactly four!");
}
 
 
[/color]