GeekyCorner
06 Jan username , password validation using javascript Posted by gayashan | 8 Comments
Iam tring to build up form validation java script. conditions are password & username cannot be empty and their size lie between 4 - 16. But this only works for empty field checking. pls help me.. (sorry for my English)


<script type=\"text/javascript\">
function validateForm() {
with (document.adduserForm) {
var alertMsg = \"Input error:\\n\";

if ((login.value == \"\") || ((login.length > 16) || (login.length < 4))) alertMsg += \"\\nlogin cannot be empty \\n and \\nlogin size 4 -16 \";

if ((password.value == \"\") || ((password.length > 16)|| (password.length < 4)))alertMsg += \"\\npassword cannot be empty \\n and \\nlogin size 4 -16 \";

if (alertMsg != \"Input error:\\n\") {
alert(alertMsg);
return false;
} else {
return true;
} } }
</script>

8 Replies to "username , password validation using javascript"

hakim ali January 15, 2013 at 7:38 pm | Reply

+2

how to validate login form in php with javascript?
Govinda Yadav November 15, 2013 at 1:07 pm

+3

function a()
{


if( document.add_user.fnm.value == "" )
{

alert( "Please enter Fullname" );
document.add_user.fnm.focus() ;
return false;
}
if( document.add_user.career.value == "" )
{

alert( "Please enter Career" );
document.add_user.career.focus() ;
return false;
}
if( document.add_user.pwd.value == "" )
{
alert( "Please enter Password" );
document.add_user.pwd.focus() ;
return false;
}

var a = document.add_user.pwd.value;
if ((a.length < 4) || (a.length > 8))
{
alert("Your Password must be 4 to 8 Character");
document.add_user.pwd.select();
return false;
}
if( document.add_user.mail.value == "" )
{
alert( "Please enter Email" );
document.add_user.mail.focus() ;
return false;
}


var x=document.forms["add_user"]["mail"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
alert("Not a valid e-mail address");
return false;
}

if( document.add_user.age.value == "" )
{
alert( "Please enter age" );
document.add_user.age.focus() ;
return false;
}
var regax = /^[0-9]+$/;
if(!regax.test(document.add_user.age.value))
{
alert( "Age must have numeric value only" );
document.add_user.age.focus() ;
return false;
}

if( document.add_user.school_id.value == "" )
{
alert( "Please enter School Id" );
document.add_user.school_id.focus() ;
return false;
}
var regax = /^[0-9]+$/;
if(!regax.test(document.add_user.school_id.value))
{
alert( "School Id must have numeric value only" );
document.add_user.school_id.focus() ;
return false;
}




if( document.add_user.description.value == "" )
{
alert( "Please enter description" );
document.add_user.description.focus() ;
return false;
}

}
akki February 11, 2013 at 8:50 am | Reply

+4

how to validate login form which contains username ,password ,retype password with java code
niralim March 7, 2013 at 4:55 am | Reply

+3

milan don't use
robski May 11, 2013 at 6:56 pm | Reply

0

hello
robski May 11, 2013 at 7:05 pm | Reply

+2

try these one:
//javascript
<script language="JavaScript">
function check() {
if (document.Form1.username.value == "") {
alert("Enter username");
document.Form1.username.focus();
return false;
}

if (document.Form1.username.value.length > 16) {
alert("Username greater than 16");
document.Form1.username.focus();
return false;
}

if (document.Form1.username.value.length < 4) {
alert("Username less than 4");
document.Form1.username.focus();
return false;
}
}

//html

<input type="submit" value="Submit" onClick="return check()">
lerato November 12, 2013 at 11:20 pm | Reply

0

Welcome
Kamal Kumar February 10, 2014 at 10:18 am | Reply

+1

Your ideas are superb. Both coding and UI are fantastic. Please send me some more validations given by Java Script.
  • 1


Please be polite and helpful and do not spam or offend others. We promise you will be treated the same way.

Log in your free account or if you still haven't joined you can create your free account now.

Posting tip:
if you use code in your comments please put it in these tags [php], [sql], [css], [js]
PHP code example: [php] echo date("Y-m-d"); [/php]

Thank you,
~ PHPJabbers team ~