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
I advice you to learn regular expressions, It will shorten your code dramaticly and will make it work faster. http://www.regular-expressions.info/javascript.html Good luck!
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
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.