Wrox Programmer Forums
|
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
 
Old July 19th, 2011, 05:48 AM
Authorized User
 
Join Date: Jul 2011
Posts: 49
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;
}
 
Old July 19th, 2011, 06:22 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 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/
 
Old July 19th, 2011, 06:28 AM
Authorized User
 
Join Date: Jul 2011
Posts: 49
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.
 
Old July 19th, 2011, 06:36 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 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/
 
Old July 19th, 2011, 06:41 AM
Authorized User
 
Join Date: Jul 2011
Posts: 49
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).
 
Old July 19th, 2011, 06:48 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Well you're allowed to test it yourself.
__________________
Joe
http://joe.fawcett.name/
 
Old July 19th, 2011, 07:12 AM
Authorized User
 
Join Date: Jul 2011
Posts: 49
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;
}
 
Old July 19th, 2011, 07:21 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You're testing against the form, not the value in the textbox.
__________________
Joe
http://joe.fawcett.name/
 
Old July 19th, 2011, 07:27 AM
Authorized User
 
Join Date: Jul 2011
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
Default reply

then what to do to work for value in the text box
 
Old July 19th, 2011, 07:48 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 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/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Password Strength Verification vinodhanandhan .NET Framework 1.x 0 December 10th, 2006 04: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 03:59 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.