 |
| Javascript General Javascript discussions. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

July 19th, 2011, 08:01 AM
|
|
Authorized User
|
|
Join Date: Jul 2011
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
reply
hi,
this function is not taking a value at all for user name.
my complete code is given below /* a simple registration form */
<html>
<head>
<meta charset="utf-8">
<title>Validation using JavaScript</title>
<script type="text/javascript">
function checkName(form) /* for real name verification */
{
var sRealName = form.realname.value;
else if (sRealName == '')
{
alert('Error: Username cannot be blank!');
form.realname.focus();
return false;
}
else if(sRealName.length < 4)
{
alert("UserName should be atleast 4 characters long");
return false;
}
var oRE =/^[\w\d-.]{4,}$/g;
var isCorrectFormat = oRE.test(sRealName);
if (!isCorrectFormat)
{
alert("Invalid characters in username. It can only contain 0-91-9._-");
return false;
}
return true;
}
function checkEmail(form) /* for email validation */
{
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form.email.value))
{
return true;
}
alert('Invalid E-mail Address! Please re-enter.');
return false;
}
function validatePwd(form) /* password & retype-password verification */
{
var invalid = ' ';
minLength = 6;
var pw1 = form.password.value;
var pw2 = form.password2.value;
if (pw1 == '' || pw2 == '')
{
alert('Please enter your password twice.');
return false;
}
if (form.password.value.length < minLength)
{
alert('Your password must be at least ' + minLength + ' characters long. Try again.');
return false;
}
if (document.form.password.value.indexOf(invalid) > -1)
{
alert('Sorry, spaces are not allowed.');
return false;
}
else
{
if (pw1 != pw2)
{
alert('You did not enter the same new password twice. Please re-enter your password.');
return false;
}
else
{
alert('Passwords Match.');
return false;
}
return false;
}
}
function validPhone(form) /* phone no validation */
{
var valid = '0123456789';
phone = form.phoneno.value;
if (phone == '')
{
alert('This field is required. Please enter phone number');
return false;
}
if (!phone.length > 1 || phone.length < 10)
{
alert('Invalid phone number length! Please try again.');
return false;
}
for (var i = 0; i < phone.length; i++)
{
temp = '' + phone.substring(i, i + 1);
if (valid.indexOf(temp) == -1)
{
alert('Invalid characters in your phone. Please try again.');
return false;
}
}
return true;
}
function validate()
{
var form = document.forms['form'];
if (!checkName(form) || !checkEmail(form) || !validatePwd(form) || !validPhone(form))
{
return false;
}
return true;
}
</script>
</head>
<body>
<form action="" method="post" name="form" onsubmit="return validate()">
User Name : <input type="text" name="realname" size="19">
<br>
E-Mail : <input type="text" name="email" size="25">
<br>
Password : <input type="password" name="password" maxlength="12" size="25">
<br>
Retype password: <input type="password" name="password2" maxlength="12" size="25">
<br>
PhoneNo : <input type="phoneno" name="phoneno" maxlength="10" size="25">
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
|
|

July 19th, 2011, 09:00 AM
|
|
Authorized User
|
|
Join Date: Jul 2011
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
reply
please can u modify that code for me because i have absolutely
no idea on regular expression..
|
|

July 19th, 2011, 09:18 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Not sure what the problem is. Just remove all the 'else' words from your checkName function.
If you are getting an error describe what it is.
|
|

July 20th, 2011, 12:04 AM
|
|
Authorized User
|
|
Join Date: Jul 2011
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
reply
I have removed all the else part but it is not giving correct output.
it is alerting every name as invalid even if we give only alphabets also
like giving ravi also it is displaying the error.
but i want to display the error other than .(dot),_(underscore),-(hypen)
special characters.
|
|

July 20th, 2011, 04:19 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Okay, this is working for me, if it doesn't for you you must be doing something else wrong. Try stepping through the code in a debugger and see where it goes wrong.
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Validate Username</title>
<script type="text/javascript">
function validateUsername(textbox)
{
var sUsername = textbox.value;
var oRE = /^[\w\d-.]{4,}$/g;
var isCorrectFormat = oRE.test(sUsername);
if (!isCorrectFormat)
{
alert("Incorrect format.");
textbox.select();
textbox.focus();
return false;
}
alert("Correct format");
return true;
}
</script>
</head>
<body>
<input type="text" id="txtUsername" size="30" maxlength="20" /> <input type="button" value="Validate" onclick="validateUsername(document.getElementById('txtUsername'));" />
</body>
</html>
|
|

July 20th, 2011, 04:31 AM
|
|
Authorized User
|
|
Join Date: Jul 2011
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
reply
i am not getting you why you have taken textbox as a parameter and created another validateusername function
|
|

July 20th, 2011, 04:40 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Well it's a proof of concept to show you that the regular expression works in isolation. If it's not working within your page then that's not the problem.
|
|

July 20th, 2011, 04:46 AM
|
|
Authorized User
|
|
Join Date: Jul 2011
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
reply
then u r telling that my code does not work with regular expression i have used
|
|

July 20th, 2011, 06:10 AM
|
|
Authorized User
|
|
Join Date: Jul 2011
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
reply
your code is working for username like ravi.de_uyt
username should accept only one special character either .(dot),_(underscore)
or -(hypen)
but your code is accepting all special characters.
whether in gmail or any other have u seen accepting mixed symbols..
|
|

July 20th, 2011, 06:26 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Okay, I misunderstood, I thought you could have more than one. In that case it's more complicated. I'll try to think of the answer but too busy at real work today...
|
|
 |