Need help in this work

Discussion in 'JavaScript' started by ravichandran, Mar 15, 2008.

  1. #1
    hello,
    i am using javascript for to make digits validation in one text box.
    the following are the allowable input for that particular text box

    1.That is in first digit should be 1
    2.the second digit should be between 1to6
    3.the third digit shoule be (.)dot
    4.forth and final digit should be between 1to9

    In that process 1 and 2 are working good.
    3 and 4 not working properly.
    my coding is as follows
    function validatecredit(x)
     {
    	var maintainplus = '';
     	var numval = x.value;
     	if (numval.charAt(0)=='1')  
        {
            alert("1");
            if(numval.charAt(1)>'0'|| numval.chatAt(1)>7)
              {
              alert("2");
                  if(numval.charAt(2))
                  { 
                  alert("3");
                              if(numval.charAt(3))
                              {
                              alert("4");
                                  if(numval.charAt(4))
                                  { 
                                  alert("5");
                                   curphonevar=numval.replace(/[\\A-Za-z!";£$%^&*+_={};:'@#~,.¦\/<>?|`¬\]\[]/g,'');
     	                            x.value = maintainplus + curphonevar;
     	                            var maintainplus = '';
                                	x.focus;
                                  return true;
                                  }
                                  else
                                  {
                                   curphonevar=numval.replace(/[\\A-Za-z!"£$%^&*+_={};:'@#~,.¦\/<>?|`¬\]\[]/g,'');
     	                            x.value = maintainplus + curphonevar;
     	                            var maintainplus = '';
     	                            x.focus;
                                  return true;                                                 
                                  }                          
                              }                
                              else
                              {
                               curphonevar=numval.replace(/[\\A-Za-z!"£$%^&*+_={};:'@#~,.¦\/<>?|`¬\]\[]/g,'');
                               	x.value = maintainplus + curphonevar;
     	                          var maintainplus = '';
     	                          x.focus;
                                return true;
                              
                              }
                  
                  }    
                  else
                  {
                   curphonevar=numval.replace(/[\\A-Za-z!"£$%^&*+_={};:'@#~,¦\/<>?|`¬\]\[]/g,'');
     	            x.value = maintainplus + curphonevar;
     	            var maintainplus = '';
     	            x.focus;
     	            return true;        
                  }
              } 
           else
              {
             
             alert("else 2");
              curphonevar=numval.replace(/[\\A-Za-z2-9!"£$%^&*+_={};0:'@#~,.¦\/<>?|`¬\]\[]/g,'');
     	x.value = maintainplus + curphonevar;
     	var maintainplus = '';
     	x.focus;
     	return true;
             }
    }
        else
        {
        alert("else 1");
        curphonevar=numval.replace(/[\\A-Za-z2-9!"£$%^&*+_={};0:'@#~,.¦\/<>?|`¬\]\[]/g,'');
     	x.value = maintainplus + curphonevar;
     	var maintainplus = '';
     	x.focus;
     	return true;
        }
       
    }
    	
    Code (markup):
    pls help us to make process 3 and 4 to work well
    plsreply me if u know the answer
     
    ravichandran, Mar 15, 2008 IP
  2. Morishani

    Morishani Peon

    Messages:
    239
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Morishani, Mar 15, 2008 IP
  3. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #3
    Is it a four digit number?

    you are checking more that four digits in this program

    Regards

    Alex
     
    kmap, Mar 15, 2008 IP
  4. ravichandran

    ravichandran Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for reply
    I have used 5 digits in my program.The text fileld should be like this:
    1.The first digit must be "1".
    2.The second digit should be between 1 to 6.
    3.The third digit must be dot(.)
    4.The forth and fifth digit should be between 0 to 9

    for example the number is: 12.90

    please help me to solve this problem
     
    ravichandran, Mar 16, 2008 IP
  5. WebGodzilla

    WebGodzilla Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    
    function validatecredit(x)
    {
    	return x.match( /^1[1-6]\.[0-9]{2}$/ );
    }
    
    Code (markup):
    If the string is what you need, the function returns the string, otherwise null. If you put it in some if() clause, it will cast to true or false, respectively.
     
    WebGodzilla, Mar 16, 2008 IP