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 .
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;
}
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'];
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.
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>
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.
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..
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...