PDA

View Full Version : Forms &*&^!!


jjjm
11-28-2004, 12:05 AM
Could you please check what is wrong with my code..
This code ask for the full name and date (not yet writen), the full name text field contents should start with a letter, contain at least one space and end with a letter; the code that I have writen is following but is not working...HELP

<html>

<head>
<script type="text/javascript">
function validate()
{
x=document.myForm
Space=x.fname.value.indexOf(" ")

fullname=x.fname.value
submitOK="True"

// Checking first character of name
if (fullname.charAt(0))!= [^a-zA-Z]//if first charachter of name is not a letter
{
alert("Not a valid name, first character should be a letter")
submitOK="False"
}


//checking last character of name
LastLetterNumber=fullname.length //to know the positon of last letter of full name

if (fullname.charAt(LastLetterNumber-1)) != [^a-zA-Z]//if last character of name is not a letter
{
alert("Not a valid name, last character should be a letter")
submitOK="False"
}


if (Space==-1)//if no space was found
{
alert("Not a valid name, we need your full name")
submitOK="False"
}

if (submitOK=="False")
{
return false
}

}//end of function
</script>
</head>

<body>
<form name="myForm" onsubmit="return validate()">
Enter your full Name: <input type="text" name="fname" size="20"><br />
Enter your date of birth: <input type="text" name="date" size="10"><br />
<input type="submit" value="Submit">
</form>
</body>

</html>

Badman3k
11-28-2004, 01:39 AM
Okay you weren't using the regex properly. I've modified the code to check for the characters properly.


<html>

<head>
<script type="text/javascript">
function validate()
{
x=document.myForm
Space=x.fname.value.indexOf(" ")

fullname=x.fname.value
submitOK="True"

// Checking first character of name
if (/[^a-z]/i.test(fullname.charAt(0)))//if first charachter of name is not a letter
{
alert("Not a valid name, first character should be a letter")
submitOK="False"
}


//checking last character of name
LastLetterNumber=fullname.length //to know the positon of last letter of full name

if (/[^a-z]/i.test(fullname.charAt(LastLetterNumber-1)))//if last character of name is

not a letter
{
alert("Not a valid name, last character should be a letter")
submitOK="False"
}


if (Space==-1)//if no space was found
{
alert("Not a valid name, we need your full name")
submitOK="False"
}

if (submitOK=="False")
{
return false
}
}//end of function
</script>
</head>

<body>
<form name="myForm" onsubmit="return validate()">
Enter your full Name: <input type="text" name="fname" size="20"><br />
Enter your date of birth: <input type="text" name="date" size="10"><br />
<input type="submit" value="Submit">
</form>
</body>

</html>

:thumbsup: