Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #11 (permalink)  
Old July 19th, 2011, 08:01 AM
Authorized User
Points: 223, Level: 4
Points: 223, Level: 4 Points: 223, Level: 4 Points: 223, Level: 4
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2011
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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>
Reply With Quote
  #12 (permalink)  
Old July 19th, 2011, 09:00 AM
Authorized User
Points: 223, Level: 4
Points: 223, Level: 4 Points: 223, Level: 4 Points: 223, Level: 4
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2011
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
Default reply

please can u modify that code for me because i have absolutely
no idea on regular expression..
Reply With Quote
  #13 (permalink)  
Old July 19th, 2011, 09:18 AM
joefawcett's Avatar
Wrox Author
Points: 9,712, Level: 42
Points: 9,712, Level: 42 Points: 9,712, Level: 42 Points: 9,712, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,066
Thanks: 1
Thanked 36 Times in 35 Posts
Default

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.
__________________
Joe
http://joe.fawcett.name/
Reply With Quote
  #14 (permalink)  
Old July 20th, 2011, 12:04 AM
Authorized User
Points: 223, Level: 4
Points: 223, Level: 4 Points: 223, Level: 4 Points: 223, Level: 4
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2011
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.
Reply With Quote
  #15 (permalink)  
Old July 20th, 2011, 04:19 AM
joefawcett's Avatar
Wrox Author
Points: 9,712, Level: 42
Points: 9,712, Level: 42 Points: 9,712, Level: 42 Points: 9,712, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,066
Thanks: 1
Thanked 36 Times in 35 Posts
Default

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" />&nbsp;<input type="button" value="Validate" onclick="validateUsername(document.getElementById('txtUsername'));" />
</body>
</html>
__________________
Joe
http://joe.fawcett.name/
Reply With Quote
  #16 (permalink)  
Old July 20th, 2011, 04:31 AM
Authorized User
Points: 223, Level: 4
Points: 223, Level: 4 Points: 223, Level: 4 Points: 223, Level: 4
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2011
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
Default reply

i am not getting you why you have taken textbox as a parameter and created another validateusername function
Reply With Quote
  #17 (permalink)  
Old July 20th, 2011, 04:40 AM
joefawcett's Avatar
Wrox Author
Points: 9,712, Level: 42
Points: 9,712, Level: 42 Points: 9,712, Level: 42 Points: 9,712, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,066
Thanks: 1
Thanked 36 Times in 35 Posts
Default

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.
__________________
Joe
http://joe.fawcett.name/
Reply With Quote
  #18 (permalink)  
Old July 20th, 2011, 04:46 AM
Authorized User
Points: 223, Level: 4
Points: 223, Level: 4 Points: 223, Level: 4 Points: 223, Level: 4
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2011
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
Default reply

then u r telling that my code does not work with regular expression i have used
Reply With Quote
  #19 (permalink)  
Old July 20th, 2011, 06:10 AM
Authorized User
Points: 223, Level: 4
Points: 223, Level: 4 Points: 223, Level: 4 Points: 223, Level: 4
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2011
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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..
Reply With Quote
  #20 (permalink)  
Old July 20th, 2011, 06:26 AM
joefawcett's Avatar
Wrox Author
Points: 9,712, Level: 42
Points: 9,712, Level: 42 Points: 9,712, Level: 42 Points: 9,712, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,066
Thanks: 1
Thanked 36 Times in 35 Posts
Default

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...
__________________
Joe
http://joe.fawcett.name/
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Password Strength Verification vinodhanandhan .NET Framework 1.x 0 December 10th, 2006 03:45 AM
login verification katie456 Access ASP 3 October 16th, 2005 01:47 PM
Email verification John K. King Javascript How-To 4 November 24th, 2004 02:59 AM



All times are GMT -4. The time now is 04:22 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.