PDA

View Full Version : Problems with validation


Xion
07-10-2003, 08:06 PM
I wrote a validation script that has to match certain characters in the username based on there email domain with our company.

var UN
UN = (document.forms["customers"].UserName.value)
var Srch
switch(document.forms["customers"].EmailDomain.value)
{
case "calif":
Srch = UN.match(".ca")
if (Srch == null) {
alert("You need to have username.ca in the Username Field");
return false;
}
break;

The problem is that its matching just "ca" and not ".ca". Anybody have any ideas on how to force it to match ".ca" ?

tobymiller
01-21-2004, 05:52 PM
Why not let the regular expressions do all of the work for you?



<html>
<head>
<title></title>
<script type="text/javascript">

function test()
{
var UN = document.forms["customers"].elements["UserName"].value;
var re = null;

switch(document.forms["customers"].elements["EmailDomain"].value)
{
case "calif":
re = /^[^\.]+\.ca$/;
if (!re.test(UN))
{
alert("You need to have username.ca in the Username Field");
return false;
}
break;
}
}

</script>
</head>
<body>
<form name="customers">
<select name="EmailDomain"><option value="calif">California</option></select>
<input type="text" name="UserName" value="yourname.ca">
<input type="button" value="test" onclick="javascript:test();">
</form>
</body>
</html>