HELP!..cannot validate other elements of the form

  • leerzej04
  • Born
  • Born
  • leerzej04
  • Posts: 1

Post 3+ Months Ago

I have here the javascript for validating the form includes: if empty, not numeric , etc..

Code: [ Select ]
<script language='javascript'>

function validate(){
var num=0;

document.getElementById('error_name').style.display='none';
document.getElementById('error_first_name').style.display='none';
document.getElementById('error_last_name').style.display='none';
document.getElementById('error_middle_name').style.display='none';
document.getElementById('error_province').style.display='none';
document.getElementById('error_country').style.display='none';
document.getElementById('error_street_add').style.display='none';
document.getElementById('error_zip_code').style.display='none';
document.getElementById('error_city').style.display='none';



if(document.getElementById('firstname').value=='' || document.getElementById('lastname').value=='' || document.getElementById('middlename').value==''){
    document.getElementById('error_name').style.display='block';
    num+=1;}

if(isName(document.getElementById('firstname').value)){
    document.getElementById('error_first_name').style.display='block';
    num+=1;}

if(isName(document.getElementById('lastname').value)){
    document.getElementById('error_last_name').style.display='block';
    num+=1;}

if(isName(document.getElementById('middleinitial').value) || (document.getElementById('middleinitial').value.length > 1) ){
    document.getElementById('error_middle_name').style.display='block';
    num+=1;}

if(document.getElementById('street').value==''){
    document.getElementById('error_street_add').style.display='block';
    num+=1;}

if((document.getElementById('zipcode').value=='') || isNumeric(document.getElementById('zipcode'))){
    document.getElementById('error_zip_code').style.display='block';
    num+=1;}
    
if((document.getElementById('city').value=='') || isName(document.getElementById('city').value)){
    document.getElementById('error_city').style.display='block';
    num+=1;}
    
if((document.getElementById('province').value=='') || isName(document.getElementById('province').value)){
    document.getElementById('error_province').style.display='block';
    num+=1;}
    
if(document.getElementById('country').value==''){
    document.getElementById('error_country').style.display='block';
    num+=1;}

    

if(num!=0 || x!=0){
    alert('The following field(s) is/are empty or invalid:\n\n'+num);
    num=0;
    return false;
}else    
    alert(num+' ' +x);
    
}
function isNumeric(elem){
    var numericExpression = /^[0-9]+$/;

    if(elem.value.match(numericExpression)){
        return false;
    }else{
        return true;
    }

}

function isName( value )
{
  if (/^[a-z]([a-z\s]*)$/i.test(value)==true)
    return false;
  else
    return true;
}
</script>
  1. <script language='javascript'>
  2. function validate(){
  3. var num=0;
  4. document.getElementById('error_name').style.display='none';
  5. document.getElementById('error_first_name').style.display='none';
  6. document.getElementById('error_last_name').style.display='none';
  7. document.getElementById('error_middle_name').style.display='none';
  8. document.getElementById('error_province').style.display='none';
  9. document.getElementById('error_country').style.display='none';
  10. document.getElementById('error_street_add').style.display='none';
  11. document.getElementById('error_zip_code').style.display='none';
  12. document.getElementById('error_city').style.display='none';
  13. if(document.getElementById('firstname').value=='' || document.getElementById('lastname').value=='' || document.getElementById('middlename').value==''){
  14.     document.getElementById('error_name').style.display='block';
  15.     num+=1;}
  16. if(isName(document.getElementById('firstname').value)){
  17.     document.getElementById('error_first_name').style.display='block';
  18.     num+=1;}
  19. if(isName(document.getElementById('lastname').value)){
  20.     document.getElementById('error_last_name').style.display='block';
  21.     num+=1;}
  22. if(isName(document.getElementById('middleinitial').value) || (document.getElementById('middleinitial').value.length > 1) ){
  23.     document.getElementById('error_middle_name').style.display='block';
  24.     num+=1;}
  25. if(document.getElementById('street').value==''){
  26.     document.getElementById('error_street_add').style.display='block';
  27.     num+=1;}
  28. if((document.getElementById('zipcode').value=='') || isNumeric(document.getElementById('zipcode'))){
  29.     document.getElementById('error_zip_code').style.display='block';
  30.     num+=1;}
  31.     
  32. if((document.getElementById('city').value=='') || isName(document.getElementById('city').value)){
  33.     document.getElementById('error_city').style.display='block';
  34.     num+=1;}
  35.     
  36. if((document.getElementById('province').value=='') || isName(document.getElementById('province').value)){
  37.     document.getElementById('error_province').style.display='block';
  38.     num+=1;}
  39.     
  40. if(document.getElementById('country').value==''){
  41.     document.getElementById('error_country').style.display='block';
  42.     num+=1;}
  43.     
  44. if(num!=0 || x!=0){
  45.     alert('The following field(s) is/are empty or invalid:\n\n'+num);
  46.     num=0;
  47.     return false;
  48. }else    
  49.     alert(num+' ' +x);
  50.     
  51. }
  52. function isNumeric(elem){
  53.     var numericExpression = /^[0-9]+$/;
  54.     if(elem.value.match(numericExpression)){
  55.         return false;
  56.     }else{
  57.         return true;
  58.     }
  59. }
  60. function isName( value )
  61. {
  62.   if (/^[a-z]([a-z\s]*)$/i.test(value)==true)
  63.     return false;
  64.   else
  65.     return true;
  66. }
  67. </script>


here's the form:


Code: [ Select ]
<form id="registration" name="registration" class="form1 page" autocomplete="off" enctype="multipart/form-data" method="post" onsubmit="return validate()" action="">
<ul>
    

<li id="foli0"         class="name   ">
    <label class="desc" id="title0" for="Field0">
        Name
        <span id="req_0" class="req">*</span>    
        <span class="req" id="error_name" style="display:none;"><em>Please complete your name</em></span>    
        <span class="req" id="error_first_name" style="display:none;"><em>Invalid first name</em></span>
        <span class="req" id="error_last_name" style="display:none;"><em>Invalid last name</em></span>
        <span class="req" id="error_middle_name" style="display:none;"><em>Invalid middle name</em></span>
            </label>
    
        <span>
        <input id="firstname"             name="firstname"             type="text"             class="field text"             value=""             size="20"             tabindex="1"         onFocus="this.value=''"                />
        <label for="Field1">First</label>
    </span>
    <span>
        <input id="lastname"             name="lastname"             type="text"             class="field text"             value=""             size="20"             tabindex="2"         onFocus="this.value=''"                />
        <label for="Field2">Last</label>
    </span>
    <span>
        <input             name="middleinitial"             type="text"             class="field text" id="middleinitial"             tabindex="3"             value=""             size="3"     onFocus="this.value=''"                    />
        <label for="Field3">MI</label>
    </span>    </li></ul></form>
  1. <form id="registration" name="registration" class="form1 page" autocomplete="off" enctype="multipart/form-data" method="post" onsubmit="return validate()" action="">
  2. <ul>
  3.     
  4. <li id="foli0"         class="name   ">
  5.     <label class="desc" id="title0" for="Field0">
  6.         Name
  7.         <span id="req_0" class="req">*</span>    
  8.         <span class="req" id="error_name" style="display:none;"><em>Please complete your name</em></span>    
  9.         <span class="req" id="error_first_name" style="display:none;"><em>Invalid first name</em></span>
  10.         <span class="req" id="error_last_name" style="display:none;"><em>Invalid last name</em></span>
  11.         <span class="req" id="error_middle_name" style="display:none;"><em>Invalid middle name</em></span>
  12.             </label>
  13.     
  14.         <span>
  15.         <input id="firstname"             name="firstname"             type="text"             class="field text"             value=""             size="20"             tabindex="1"         onFocus="this.value=''"                />
  16.         <label for="Field1">First</label>
  17.     </span>
  18.     <span>
  19.         <input id="lastname"             name="lastname"             type="text"             class="field text"             value=""             size="20"             tabindex="2"         onFocus="this.value=''"                />
  20.         <label for="Field2">Last</label>
  21.     </span>
  22.     <span>
  23.         <input             name="middleinitial"             type="text"             class="field text" id="middleinitial"             tabindex="3"             value=""             size="3"     onFocus="this.value=''"                    />
  24.         <label for="Field3">MI</label>
  25.     </span>    </li></ul></form>


what happens is that once the firstname, lastname, and middlename have been validated with no errors, it won't validate the other elements anymor..instead it directly links to the other..
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

Post Information

  • Total Posts in this topic: 1 post
  • Users browsing this forum: No registered users and 162 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.