checking textbox field

HTML, CSS and anything else that deals with client side capabilities.

Moderator: General Moderators

Post Reply
rrn
Forum Commoner
Posts: 46
Joined: Wed Apr 15, 2009 7:54 am

checking textbox field

Post 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......
Last edited by Benjamin on Tue May 26, 2009 11:20 am, edited 1 time in total.
Reason: Changed code type from text to javascript.
oliur
Forum Commoner
Posts: 29
Joined: Tue May 26, 2009 3:43 am

Re: checking textbox field

Post 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]
Last edited by Benjamin on Tue May 26, 2009 11:20 am, edited 1 time in total.
Reason: Changed code type from text to javascript.
Post Reply