/ Published in: JavaScript
                    
                                        
This snippet contains a text input field called 'abc' and we calling the checkNum function as the onclick action of a button 'btn'
                
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
function checkNum(){
var inputBox = document.getElementById('abc');
if(inputBox.value.length!='0'){
var j=0;
var run = false;
for(j=0; j<inputBox.value.length; j++){
var char=inputBox.value.charAt(j);
if(inputBox.value.charAt(j)>='0' && inputBox.value.charAt(j)<='9'){
run = true;
}
else{
run = false;
alert("Q# can only be a numeric value");
document.getElementById('abc').focus();
document.getElementById('abc').value="";
break;
}
}
if(run){
alert("The field has been successfully validated!");
}
}
else if(inputBox.value.length == '0'){
alert("Please enter a Q#");
document.getElementById('abc').focus();
}
}
Comments
 Subscribe to comments
                    Subscribe to comments
                
                