condtion in javascript validation not working properly -
condtion in javascript validation not working properly -
i have textbox , submit button
<input type="text" name="username" id="username"/><br/> <input type="submit" onclick="return validate()"/>
here function code:
if (document.getelementbyid("usernameee").value == null || document.getelementbyid("usernameee").value == "" ) { document.getelementbyid("usernameee").style.bordercolor = 'red'; homecoming false } else { document.getelementbyid("usernameee").style.bordercolor = ''; if (document.getelementbyid("usernameee").value.length!=0 || document.getelementbyid("usernameee").value.length < 8 ) { document.getelementbyid("usernameee").style.bordercolor = 'red'; document.getelementbyid("message").innerhtml="enter atleast 8 characters" homecoming false } else { document.getelementbyid("usernameee").style.bordercolor = ''; }
now want if user leaves filed empty should highlight textbox backgound color reddish , if username less 8 characters should show message , highlight backgound of textbox if textbox empty displaying message dont want...if field empty should ol
i think sec conditional wrong. can write code much cleaner this:
var username = document.getelementbyid("usernameee"); if(!username.value) { //there no username username.style.bordercolor = 'red'; } else if(username.value.length < 8) { //there username , value shorter 8 characters username.style.bordercolor = 'red'; document.getelementbyid("message").innerhtml = "enter atleast 8 characters"; } else { //there username , longer or equal 8 characters. username.style.bordercolor = ''; document.getelementbyid("message").innerhtml = ""; }
javascript
Comments
Post a Comment