/    Sign up×
Community /Pin to ProfileBookmark

Validating a form with values already defined

I have the following script:

[CODE]function validateForm()
{
var form = document.getElementById(“productform”)
var elementNum = 12
var errorString = “You have not filled in the following fields:n”
var i
var error = false

for(i=0;i<elementNum;i++)
{
if (document.forms[0].elements[i].value == “”)
{
errorString = errorString + ” – ” + document.createproductfrm.elements[i].name + “n”
error = true
}
}

if (error == true)
{
window.alert(errorString)
return false;
}
else
{ return true; }
}[/CODE]

When creating a product on my page, the script goes through the input boxes and makes sure a value is present. This works fine.

However, I am currently using the same script to validate input boxes when I update a product. When updating a product, my background PHP puts the current details from the database into the text boxes on the form.

My script does not recognise when a value has been deleted from a text box by a user. Although when viewing in the browser, the box appears blank, the value of the box is still present in HTML (because it was set my PHP code). Because the value is not “”, my script thinks everything is ok, even though the user has left a field blank. Any suggestions?

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @markyoung1984 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 6.16,
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: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

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