please help write a javascript function to do a client side validation of the html form i have displayed below.
the validation should check if input box Student No cannot be empty; if the txtStdNo box is empty there should be a message "Cannot be empty" and move the cursor to the txtStdNo input box.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form name='frm1' method="post" action="student.php">
	Student No <input type="text" name='txtStdNo' /> <BR />
    Name <input type="text" name='txtName' /> <BR />
    Address <input type="text" name='txtAdd' /> <BR />
    Contact No <input type="text" name='txtTel' /> <BR />
    <input type="submit" name="cmdDelete" value="Delete" /> 
    <input type="submit" name="cmdRetrieve" value="Retrieve" /> 
    <input type="submit" name="cmdUpdate" value="Update" /> 
    <input type="submit" name="cmdClose" value="Close" /> 
    </form>
</body>
</html>

please help write HTML code to attach the javascript function to the form. form should not be submitted if STDNO box is empty.

HOW DO YOU DO THIS???

THANK YOU

You'll need to add an onsubmit event to your form tag that can run a validation function. Function should return false if txtStdNo is blank. There are plenty of tutorials on this subject...here are a couple...
http://articles.sitepoint.com/article/form-validation-client-side
http://www.shiningstar.net/articles/articles/javascript/javascriptvalidations.asp?ID=aw
Also, when it fails you'll have to use alert("message") for your message box and focus() on your input box to set the focus. Try it out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.