Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have to write a Javascript function that checks a form from an online store wh

ID: 3626054 • Letter: I

Question

I have to write a Javascript function that checks a form from an online store when the submit button is pressed.
It has to recognize a specific username and password.This part is easy.
Then its has to make sure that the three text fields where you input the quantity of the item your ordering obeys some rules:
1- There cant be a negative number on any field
2- You cant leave all text fields blank(the user must order something)
3- You cant type zero into all text fields(the user must order something)

So far I can make it so that you cant have any negative numbers and you have to write something on all boxes. How can I make it so that for example I leave one boxe empty and other two filled and the form is considered valid?

Thanks for your help.

Explanation / Answer

<html>
<head>
<title> Input Validation </title>

<script languge="JavaScript">

function validateForm (form)
{
validate(form);
var name=form.username.value;
var pass=form.txtpassword.value;
if(name == "admin" && pass == "admin")
{
window.location="../sr.htm";
}
else
alert("Invalid login credentials");
}

function validate(form)
{
var v=form.quan.value;
if(v<0)
alert("quantity can not be negitive");
else if(v=="")
alert("quantity can not be empty");
else if(v==0)
alert("quantity can not be zero");
}

</script>
</head>

<body>
<form >

User name: <input type="text" name="username"/></br>
Password: <input type="password" name="txtpassword"/></br>
Quantity: <input type="text" name="quan"></br>

<input type="submit" name="button" value="Submit" / >

</form>
</body>
</html>