Hai Freinds, i have a javascript problem... plz try to solve it soon urgent its for numeric field validation function IsNumeric(sText){ var ValidChars = "0123456789,"; var IsNumber=true; var Char; for (i = 0; i < sText.length && IsNumber == true; i++){ Char = sText.charAt(i); if (ValidChars.indexOf(Char) == -1){ IsNumber = false; } } return IsNumber; } presently we are using the above javascript. we allow numbers and commas now the requirment is - the format should be 1,234,567 or 123,456 or 12,345,678 only max 8 numbers should be allowed in textbox but we are setting the size of textbox as 10 to allow 2 commas also... also we should not allow 2 commas together. can u find a solution and send it back
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> <script language="javascript"> /** Here is your actual function, I'll walk you through it **/ function is_valid_number( int ) { /** Maybe not the best way, but a way to count the number of characters in a string, note it's offset by +1 **/ if( int.split(',').length > 3 ) { alert("Number contains more than two commas [ "+int+" ]"); return false; } /** Get the length of the string without commas in **/ else if( int.replace( /,/g, '').length > 8 ) { alert("Number should not be more than 8 decimal digits [ "+int+" ]"); return false; } /** Check for commas next to each other **/ else if( int.match( /,,/i ) ) { alert("Number should not have two commas next to each other [ "+int+" ]"); return false; } /** Check the string has no alphabetical characters in **/ else if( int.match( /[[a-zA-Z]/ ) ) { alert("Numbers should not have any alphabetical characters in them"); return false; } /** More else if()s here if you wanted **/ return true; } /** You do not need this function, it's just a demo of the above one **/ function check_these_numbers( ) { var ints = new Array( 'Hello', '1,234,567 ', '123,456', '12,345,678', '1,,5315', '123,45,8,9', '12345678910' ); counter = null; for( counter = 0; counter < ints.length; counter++ ) { if( is_valid_number( ints[counter] ) ) { alert( 'The number : ' + ints[counter] + ' passed validation' ); } else { alert( 'The number : ' + ints[counter] + ' failed validation' ); } } } window.onload=check_these_numbers( ); </script> </head> <body> </body> </html> HTML: