 |
| 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, 05:48 AM
|
|
Authorized User
|
|
Join Date: Jul 2011
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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;
}
|
|

July 19th, 2011, 06:22 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
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.
|
|

July 19th, 2011, 06:28 AM
|
|
Authorized User
|
|
Join Date: Jul 2011
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
reply
k,can u give me with out using regular expression so that the
above written should fit with it.
|
|

July 19th, 2011, 06:36 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
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;
}
|
|

July 19th, 2011, 06:41 AM
|
|
Authorized User
|
|
Join Date: Jul 2011
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
are you sure that it will accept characters a-z and numbers 0-9
and three special characters .(dot),-(hypen) and_(underscore).
|
|

July 19th, 2011, 06:48 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Well you're allowed to test it yourself.
|
|

July 19th, 2011, 07:12 AM
|
|
Authorized User
|
|
Join Date: Jul 2011
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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;
}
|
|

July 19th, 2011, 07:21 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
You're testing against the form, not the value in the textbox.
|
|

July 19th, 2011, 07:27 AM
|
|
Authorized User
|
|
Join Date: Jul 2011
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
reply
then what to do to work for value in the text box
|
|

July 19th, 2011, 07:48 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
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;
}
|
|
 |