3 Replies - 1025 Views - Last Post: 02 March 2011 - 05:22 AM

#1 liquid_chaos  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 19-August 07

javascript validation(probably something stupid

Posted 01 March 2011 - 03:13 PM

hi, my javascript validation is being funny, if i leave any of the fields blank, it shows the errors properly but if i fill them all in but make deliberate mistakes, nothing comes up. can anyone see if ive done something wrong?

function validateUsername(field)
{
    if (field == "") return "No username was entered."
    else if (field.length<5)
        return "Usernames must be at least 5 characters."
    else if (/[a-zA-Z0-9_-]/.test(field))
        return "Only a-z, A-Z, 0-9, - and _ are allowed in usernames."
    return ""
}

function validatePassword(field)
{      
    if (field =="") return "no password was entered."
    else if (field.length<6)
        return "Passwords must contain at least 6 characters. "
    return ""
}

function validateEmail(field)
{
    if (field == "") return "No email was entered."
    else if  (!((field.indexof(".") > 0) &&
                (field.indexof("@") > 0)) ||
                /[^a-zA-Z0-9.@_-]/.test(field))
    return "The email address is invalid."
    return ""
}

function validate(form)
{
    fail  = validateUsername(form.user.value)
    fail += validatePassword(form.pass.value)
    fail += validateEmail(form.email.value)
    if (fail == "") return true
    else { alert(fail); return false }
}

<form name='signup' method='post' action='signup.php' onsubmit='return validate(this)'>$error
Username <input type='text' maxlength='16' name='user' value='$user'
	onblur='checkUser(this)'/><span id='info'></span><br />
Password <input type='password' maxlength='16' name='pass'
	value='$pass' /><br />
E-mail   <input type='text' maxlength='32' name='email' value='$email'/> <br /><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
<input type='submit' value='Signup' />
</form>



thanks for any help

Is This A Good Question/Topic? 0
  • +

Replies To: javascript validation(probably something stupid

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 948
  • View blog
  • Posts: 2,357
  • Joined: 15-February 11

Re: javascript validation(probably something stupid

Posted 01 March 2011 - 08:47 PM

i changed it up a whole lot but it works though

<html>
<head><title>@echo codeprada</title>
<script type="text/javascript">
var error = "";
function validateUsername(field)
{
	var exp = /[a-zA-Z0-9_-]+/;
	if (field.length >= 5 && field.search(exp) > -1)
		return true;
	else
		error += "Usernames must be at least 5 characters and Only a-z, A-Z, 0-9, - and _ are allowed in usernames.\n";
	return false;
}

function validatePassword(field)
{      
    if(field.length >= 6)
		return true;
	else
		error += "Passwords must contain at least 6 characters.\n";
	return false;
}

function validateEmail(field)
{
	var exp = /^([a-zA-Z]([a-zA-Z0-9_-])+)@{1}([a-zA-z]+)\.{1}[a-z]+$/;
	if(field.search(exp) > -1)
		return true;
	else
		error += "The email address is invalid.";
	return false;
}

function validate()
{
	error = "";
	var f = document.forms['signup'];
	if (validateUsername(document.getElementById("user").value) && 
		validatePassword(document.getElementById("pass").value) &&
		validateEmail(document.getElementById("email").value)) 
			f.submit();
	else
		alert(error);
}
</script>
</head>

<body>
<form name='signup' id="signup" method='post' action='signup.php'>
Username <input type='text' maxlength='16' id='user' value='user'
	onblur='checkUser(this)'/><span id='info'></span><br />
Password <input type='password' maxlength='16' id='pass'
	value='pass' /><br />
E-mail   <input type='text' maxlength='32' id='email' value='email'/> <br /><br />
</form><button onclick="validate()">Login</button>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#3 liquid_chaos  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 19-August 07

Re: javascript validation(probably something stupid

Posted 02 March 2011 - 02:50 AM

strange, yours worked but i hhad to get rid of the \n's for the errors.

im on a php document and my script tags are between <<<_end and _end tags. \n never seems to work for me

anyway your way worked perfectly, thanks :)
Was This Post Helpful? 0
  • +
  • -

#4 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 948
  • View blog
  • Posts: 2,357
  • Joined: 15-February 11

Re: javascript validation(probably something stupid

Posted 02 March 2011 - 05:22 AM

oh that's why you had $error, $pass and $user
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1