Forum Moderators: open

Message Too Old, No Replies

Newbe

can't find problem

         

kvic

10:14 pm on Nov 1, 2008 (gmt 0)

10+ Year Member



Hi
I have just started with JavaScript and created a validation page for fun, but it's not funny any more

I get a error on page and when I stop getting that message my else if doesn't work

Any guideance about the way i have written my code and a solution to my problems will be grateful
<html>
<head>
<title>Validation</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script type="text/javascript">
<!--
function FirstNameValidator()
{
try
{
var lettersOnlyWithApostrophe = /^[a-zA-Z''-'\s]{1,40}$/g;
var validateFirstName = document.getElementById("inputFirstName");

if (validateFirstName.length == 0 ¦¦ validateFirstName.value == "" )
{
alert("You must add your first name.");
}
else if (validateFirstName.match(lettersOnlyWithApostrophe) == null)
{
alert("Test");
alert(validateFirstName.match(lettersOnlyWithApostrophe) == null);
}
else
{
alert(lettersOnly.test(validateFirstName) == null);
}
}
catch(err)
{
alert("There are Error\'s on the Page");
}
}
//-->
</script>

</head>

<body>
<form name="form1" method="post" action="">
<table width="200" border="1" align="center">
<tr><td><input type="text" name="inputFirstName"></td></tr>
<tr><td><input type="submit" name="Submit" value="View Message" onClick="FirstNameValidator()" /></td></tr>
</table>
</form>
</body>
</html>

daveVk

3:45 am on Nov 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to forum

First I would leave the try/catch out and let the browser report the error location

if (validateFirstName.length == 0 ¦¦ validateFirstName.value == "" )

You dont need both tests they are the same.

var validateFirstName = document.getElementById("inputFirstName")

You want the value of the element not the element itself

var validateFirstName = document.getElementById("inputFirstName").value;

kvic

8:24 am on Nov 2, 2008 (gmt 0)

10+ Year Member



re-wrote code as per your advice but no different?
function FirstNameValidator()
{
var lettersOnlyWithApostrophe = "^[a-zA-Z''-'\s]{1,40}$";
var validateFirstName = document.getElementById("inputFirstName").value;

if (validateFirstName.length == 0)
{
alert("You must add your first name.");
}
else if (validateFirstName.match(lettersOnlyWithApostrophe) == false)
{
alert("Test");
}
}

daveVk

10:45 am on Nov 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What happens ? the "Test" alert or error message maybe ?

I am no expert on regular expressions but the one in your first post looks best.

Add a few more alerts say

alert( validateFirstName );
alert( validateFirstName.match(lettersOnlyWithApostrophe) );
alert( validateFirstName.match(lettersOnlyWithApostrophe)[0] );

null is the correct test here
else if (validateFirstName.match(lettersOnlyWithApostrophe) === null )

just noticed
<input type="text" name="inputFirstName">
should be
<input type="text" name="inputFirstName" id="inputFirstName">

p.s. make sure you have script error notification turned on, in IE tools -> internet options -> advanced

kvic

9:06 pm on Nov 2, 2008 (gmt 0)

10+ Year Member



Re-wrote the block and found your suggestion correct
it mostly works now but ignores the if statement and goes to the else if instead when you click the button without characters.

The alert states that the empty textbox has a value of null which is correct for the else if
alert(validateFirstName); = null

when you add just a-z characters then it goes to the else

if you add 123 to the textbox it goes to the elseif which is correct

<html>
<head>
<title>Validation</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script type="text/javascript">

function FirstNameValidator()
{
try
{
var lettersOnlyWithApostrophe = ("^[a-zA-Z''-'\s]{1,40}$");
var validateFirstName = document.getElementById("inputFirstName").value;

if (validateFirstName.value == "")
{
alert("You must add your first name.");
}
else if (validateFirstName.match(lettersOnlyWithApostrophe) == null)
{
alert("This is not a Valid First Name");
alert(validateFirstName.match(lettersOnlyWithApostrophe));
}
else
{
alert("This is Correct");
alert(validateFirstName);
}
}
catch(err)
{
alert("There are Error\'s on the Page");
}
}

</script>

</head>

<body>
<form name="form1" method="post" action="">
<table width="200" border="1" align="center">
<tr><td><input type="text" name="txtInputFirstName" id="inputFirstName"></td></tr>
<tr><td><input type="submit" name="Submit" value="View Message" onClick="FirstNameValidator()" /></td></tr>
</table>
</form>
</body>
</html>

kvic

9:13 pm on Nov 2, 2008 (gmt 0)

10+ Year Member



Thank's DaveVK
change the if if (validateFirstName.length == 0)
this seem's to have the script working correctly
please if you see something else that I could do to improve on what is written or a better solution
please advise
kvic
otherwise this solution is resolved

daveVk

11:18 pm on Nov 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Look good, if (validateFirstName === "" ), without .value also Ok.

I prefer using the exact equality test === instead of ==, == includes some odd conversion rules

You may find this thread of interest [webmasterworld.com...]