/    Sign up×
Community /Pin to ProfileBookmark

Javascript: Validation issue/error

I get the following error –
Error: phone.value is null or not an object

I bolded where it says the problem is. I guess I am confused as to why the error is occuring as similar code is on another page and works fine. I know the html code is not in depth but its just testing. The same error will occor on email.value. Any help would be appreciated.

My Code is –

<html>
<HEAD>
<script>

function submitIt(myForm) {

//check that fields are filled out
if (myForm.name.value == “”) {
alert(“You must enter a name”)
myForm.name.focus()
myForm.name.select()
return false
}

[B]if(!stringFilter(myForm.phone.value)){[/B]
alert(“Invalid phone number”)
myForm.phone.focus()
myForm.email.select()
return false
}

[B]if (!validEmail(myForm.email.value)) {[/B]
alert(“Invalid email address”)
myForm.email.focus()
myForm.email.select()
return false
}

function validEmail(email) {

invalidChars = ” /:,;”

if (email == “”) {
return false

}

for (i=0; i<invalidChars.length; i++) {
badChar = invalidChars.charAt(i)
if (email.indexOf(badChar,0) > -1) {
return false

}
}

atPos = email.indexOf(“@”,1)
if (atPos == -1) {
return false
}

if (email.indexOf(“@”,atPos+1) > -1) {
return false
}

periodPos = email.indexOf(“.”,atPos)
if (periodPos == -1) {
return false
}

if (periodPos+3 > email.length) {
return false
}
return true

}
}

function stringFilter (input)
{
s = input.value;
if (s == “”)
{
return false;
}
else
{
filteredValues = “()-+”; // Characters stripped out
var i;
var returnString = “”;
for (i = 0; i < s.length; i++)
{ // Search through string and append to unfiltered values to returnString.
var c = s.charAt(i);
if (filteredValues.indexOf(c) == -1) returnString += c;
}

// After stripping out (), -, or + checking to see if any characters exist. If so then fail and alert user
if(isNaN(returnString))
{
return false;
}
else
{
// Verifying that phone number is 10 digits or greater to be a valid phone number
if(returnString.length >= 10)
{
return true;
input.value = returnString;
}
else
{
return false;
}
}
}

}
</script>
</HEAD>

<BODY>
<form method=post action=”” onSubmit=””>
<input type=”text” size=”14″ maxlength=”14″ name=”name”>
<br>
<input type=”text” size=”14″ maxlength=”14″ name=”phone”>
<br>
<input type=”text” size=”14″ maxlength=”14″ name=”email”>
<br>
<input type=”button” value=”Submit” onClick=”return submitIt(this);”>
<input type=”reset” value=”Reset”>
</form>
</body>
</html>

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@Orc_ScorcherSep 13.2007 — Your submitIt function wants a reference to a form element but you're passing it an input button instead.
Copy linkTweet thisAlerts:
@mfrailauthorSep 13.2007 — What needs to be changed?

I tried to change it to

<form method=post action="" onSubmit="return submitIT(this)">

and it does nothing.
Copy linkTweet thisAlerts:
@mfrailauthorSep 13.2007 — I changed it to this ...

<input type="button" value="Submit" onClick="return submitIt(this.form)">


Now it validates my first part name ... but does nothing on the other two things. Any thoughts?

I have gotten both other functions to work outside of combining them.
Copy linkTweet thisAlerts:
@mfrailauthorSep 13.2007 — I have fixed the issue and it works. Could someone just look this over and possibly give some tips on how to better write this?

<html>

<HEAD>

<script>

function submitIt() {

//check that fields are filled out

if (myForm.name.value == "") {

alert("You must enter a name")

document.myForm.name.focus()

document.myForm.name.select()

return false

}

if(!stringFilter(myForm.phone.value)) {

alert("Invalid phone number")

document.myForm.phone.focus()

document.myForm.phone.select()

return false

}

if (!validEmail(myForm.email.value)) {

alert("Invalid email address")

document.myForm.email.focus()

document.myForm.email.select()

return false

}

function validEmail(email) {

invalidChars = " /:,;"

if (email == "") {

return false

}

for (i=0; i<invalidChars.length; i++) {

badChar = invalidChars.charAt(i)

if (email.indexOf(badChar,0) > -1) {

return false

}

}

atPos = email.indexOf("@",1)

if (atPos == -1) {

return false

}

if (email.indexOf("@",atPos+1) > -1) {

return false

}

periodPos = email.indexOf(".",atPos)

if (periodPos == -1) {

return false

}

if (periodPos+3 > email.length) {

return false

}

return true

}

}

function stringFilter (phone)

{

if (phone == "" || phone == null)

{

return false;

}

else

{

filteredValues = "()-+"; // Characters stripped out

var i;

var returnString = "";

for (i = 0; i < phone.length; i++)

{ // Search through string and append to unfiltered values to returnString.

var c = phone.charAt(i);

if (filteredValues.indexOf(c) == -1) returnString += c;

}

// After stripping out (), -, or + checking to see if any characters exist. If so then fail and alert user

if(isNaN(returnString))

{

return false;

}

else

{

// Verifying that phone number is 10 digits or greater to be a valid phone number

if(returnString.length >= 10)

{

return true;

input.value = returnString;

}

else

{

return false;

}

}

}

}

</script>

</HEAD>


<BODY>

<form name="myForm" method=post action="" onSubmit="">

<input type="text" size="14" maxlength="14" name="name">

<br>

<input type="text" size="14" maxlength="14" name="phone">

<br>

<input type="text" size="14" maxlength="14" name="email">

<br>

<input type="button" value="Submit" onClick="return submitIt()">

<input type="reset" value="Reset">

</form>

</body>

</html>
×

Success!

Help @mfrail spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...