javascript - Selection making a field mandatory -
i new javascript , have been doing university assignment based around creating form in html , javascript. in assignment have been asked make field mandatory based on whether specific option chosen in previous drop-down box.
if 'uws student' or 'uws staff' selected drop-down list field 'student number/staff number' becomes mandatory otherwise not mandatory field
if 'uws student' or 'uws staff' selected field 'institution/company' must automatically filled in 'university of western sydney'
here segment have been using:
<b>registration category:</b> <select> <option value="uws student">uws student</option> <option value="student @ institution">student @ institution</option> <option value="uws academic">uws academic</option> <option value="other uws staff">other uws staff</option> <option value="academic institution">academic institution</option> <option value="professional">professional</option> <option value="retired">retired</option> </select> <b>student number/staff number:</b> <input type="text" name="studentnumber/staffnumber"> <br> <b>institution/company:</b> <input type="text" id="txtinstcomp" name="institutioncompany"> </fieldset>
note: have function on 'institution/company' field previous question make filed mandatory, if matters @ all:
function validatetext() { var institutioncompany=document.getelementbyid('txtinstcomp'); if (institutioncompany.value=="") { alert("institution/company must filled out"); return false; } }
any appreciated struggling grasp of javascript
you must add id attributes select , input text fields below.
<b>registration category:</b> <select id="cat" onchange="populateinstitution();"> <option value="uws student">uws student</option> <option value="student @ institution"> student @ institution</option> <option value="uws academic">uws academic</option> <option value="other uws staff">other uws staff</option> <option value="academic institution"> academic institution</option> <option value="professional">professional</option> <option value="retired">retired</option> </select> <br> <b>student number/staff number:</b> <input type="text" id="stnumber" name="studentnumber/staffnumber"> <br> <b>institution/company:</b> <input type="text" id="txtinstcomp" name="institutioncompany"> <br> <input type="button" value="submit" onclick="validatetext()" />
and have javascript function 'populateinstitution' change event on select field
function validatetext() { var institutioncompany = document.getelementbyid('txtinstcomp').value; if (institutioncompany == "") { alert("institution/company must filled out"); return false; } var cat = document.getelementbyid('cat').value; if (cat == "uws student" || cat == "other uws staff") { if (document.getelementbyid("stnumber").value == "") { alert('studentnumber/staffnumber mandatory'); return; } } } function populateinstitution() { var cat = document.getelementbyid('cat').value; if (cat == "uws student" || cat == "other uws staff") { document.getelementbyid('txtinstcomp').value='university of western sydney'; }else{ document.getelementbyid('txtinstcomp').value=''; } }
Comments
Post a Comment