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
  #1 (permalink)  
Old July 19th, 2011, 05:48 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 username verification

Hi all,
I have written a function for accepting name and should be atleast minimum 4 characters.
It is accepting all characters including special characters.
Now i want my function to accept only a-z0-9 and _(underscore),-(hyphen)
and .(dot)
Below is my function

function checkName(form) /* for real name verification */
{
if (form.realname.value == '')
{
alert('Error: Username cannot be blank!');
form.realname.focus();
return false;
}
else if(form.realname.value.length < 4)
{
alert("UserName should be atleast 4 characters long");
return false;
}

return true;
}
Reply With Quote
  #2 (permalink)  
Old July 19th, 2011, 06:22 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

I think you need a regular expression:
Code:
function testUsername(text)
{
  var oRE = /^[\w\d-.]{4,}$/g;
  var isCorrectFormat = oRE.test(text);
  //alert(isCorrectFormat);
  return isCorrectFormat;
}
I haven't tested the above.
__________________
Joe
http://joe.fawcett.name/
Reply With Quote
  #3 (permalink)  
Old July 19th, 2011, 06:28 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

k,can u give me with out using regular expression so that the
above written should fit with it.
Reply With Quote
  #4 (permalink)  
Old July 19th, 2011, 06:36 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

You can just add my bit after your current code.
Code:
function checkName(form) /* for real name verification */
{
if (form.realname.value == '') 
{
alert('Error: Username cannot be blank!');
form.realname.focus(); 
return false;
}
else if(form.realname.value.length < 4)
{
alert("UserName should be atleast 4 characters long"); 
return false;
}
  var oRE = /^[\w\d-.]{4,}$/g;
  var isCorrectFormat = oRE.test(text);
  if (!isCorrectFormat)
  {
    alert("Invalid characters in username. It can only contain...");
    return false;
  }
return true;
}
__________________
Joe
http://joe.fawcett.name/
Reply With Quote
  #5 (permalink)  
Old July 19th, 2011, 06:41 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

are you sure that it will accept characters a-z and numbers 0-9
and three special characters .(dot),-(hypen) and_(underscore).
Reply With Quote
  #6 (permalink)  
Old July 19th, 2011, 06:48 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 you're allowed to test it yourself.
__________________
Joe
http://joe.fawcett.name/
Reply With Quote
  #7 (permalink)  
Old July 19th, 2011, 07:12 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

hi dude the function is not working
it is accepting other functions such as !,@,# and so on other than .,_,-
below is the code i have written..

function checkName(form) /* for real name verification */
{
var oRE =/^[\w\d-.]{4,}$/g;
var isCorrectFormat = oRE.test(form);
if (!isCorrectFormat)
{
alert("Invalid characters in username. It can only contain 0-91-9._-");
return false;
}

else if (form.realname.value == '')
{
alert('Error: Username cannot be blank!');
form.realname.focus();
return false;
}
else if(form.realname.value.length < 4)
{
alert("UserName should be atleast 4 characters long");
return false;
}


return true;
}
Reply With Quote
  #8 (permalink)  
Old July 19th, 2011, 07:21 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

You're testing against the form, not the value in the textbox.
__________________
Joe
http://joe.fawcett.name/
Reply With Quote
  #9 (permalink)  
Old July 19th, 2011, 07:27 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 what to do to work for value in the text box
Reply With Quote
  #10 (permalink)  
Old July 19th, 2011, 07:48 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

Code:
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;
}
__________________
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:29 PM.


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