Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Switch Statement Read Only

Status
Not open for further replies.

Owner68

Programmer
Mar 20, 2012
1
0
0
US
I need help with my javascript. I need the switch statement to make the fields readonly when the correct radio button is select in the form. Thanks

<html>
<h1> Contact From </h1>


<script type='text/javascript'>

function formValidator(){
// Make quick references to our fields
var phonenumber = document.getElementById('phonenumber');
var firstname = document.getElementById('firstname');
var middlename = document.getElementById('middlename');
var lastname document.getElementById('lastname');
var zip = document.getElementById('zip');
var state = document.getElementById('state');
var city = document.getElementById('city');

// Check each input in the order that it appears in the form!
if(isNumeric(phonenumber, "Plese enter numbers only (no special characters) for your phonenumber")){
if(isAlphabet(firstname, "Please enter letter only for your name")){
if(isAlphabet(middlename, "Please enter letters only for your name")){
if(isAlphabet(lastname, "Please enter letters only for you name")){
if(isNumeric(zip, "Please enter a valid zip code")){
if(madeSelection(state, "Please Choose a State")){
if(lengthRestriction(phonenumber, 10)){
if(lengthRestriction(zip, 5)){
return true;
}
}
}
}
}
}
}
}

return false;

switch(rbtn)
{
case "add":
break;
case "lookup":
document.getElementById('firstname').readOnly = true;
document.getElementById('middlename').readOnly = true;
document.getElementById('lastname').readOnly = true;
document.getElementById('middlename').readOnly = true;
document.getElementById('zip').readOnly = true;
document.getElementById('state').readOnly = true;
document.getElementById('city').readOnly = true;
break;
case "update":
document.getElementById('phonenumber').readOnly = true;
break;
case "delete":
document.getElementById('firstname').readOnly = true;
document.getElementById('middlename').readOnly = true;
document.getElementById('lastname').readOnly = true;
document.getElementById('middlename').readOnly = true;
document.getElementById('zip').readOnly = true;
document.getElementById('state').readOnly = true;
document.getElementById('city').readOnly = true;
break;
}

}

function notEmpty(elem, helperMsg){
if(elem.value.length == 0){
alert(helperMsg);
elem.focus(); // set the focus to this input
return false;
}
return true;
}

function isNumeric(elem, helperMsg){
var numericExpression = /^[0-9]+$/;
if(elem.value.match(numericExpression)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function isAlphabet(elem, helperMsg){
var alphaExp = /^[a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function isAlphanumeric(elem, helperMsg){
var alphaExp = /^[0-9a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function lengthRestriction(elem, min, max){
var uInput = elem.value;
if(uInput.length >= min && uInput.length <= max){
return true;
}else{
alert("Please enter between " +min+ " and " +max+ " characters");
elem.focus();
return false;
}
}

function madeSelection(elem, helperMsg){
if(elem.value == "Please Choose"){
alert(helperMsg);
elem.focus();
return false;
}else{
return true;
}
}

function emailValidator(elem, helperMsg){
var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
if(elem.value.match(emailExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
</script>
<form action='phonebook.php' method='POST' id='insert'>
<form onsubmit='return formValidator()' >
<table>
<tr><td>Add</td><td><input type='radio' value='add' name='rbtn' id='rbtn'/><br /></td></tr>
<tr><td>Lookup</td><td><input type='radio' value='lookup' name='rbtn' id='rbtn'/><br /></td></tr>
<tr><td>Update</td><td><input type='radio' value='update' name='rbtn' id='rbtn'/><br /></td></tr>
<tr><td>Delete</td><td><input type='radio' value='delete' name='rbtn' id='rbtn'/><br /></td></tr>
<tr><td>Phone Number:</td><td><input type='text' id='phonenumber' size='40'/><br /></td></tr>
<tr><td>First Name:</td><td><input type='text' id='firstname' size='40'/><br /></td></tr>
<tr><td>Middle Name:</td><td><input type='text' id='middlename' size='40'/><br /></td></tr>
<tr><td>Last Name:</td><td><input type='text' id='lastname' size='40'/><br /></td></tr>
<tr><td>City:</td><td><input type='text' id='city' size='40'/><br /><td></tr>
<tr><td>State:</td><td><select id='state'>
<option>Please Choose</option>
<option>AL</option>
<option>CA</option>
<option>TX</option>
<option>WI</option>
<option>UT</option>
<option>NV</option>
<option>FL</option>
<option>NM</option>
<option>CO</option>
<option>Other</option>
</select><br /></td></tr>
<tr><td>Zip Code:</td><td><input type='text' id='zip' size='40'/><br /></td></tr>
</table>
<input type='submit' value='SUBMIT' />
<input type='reset' value='RESET' />
</form>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top