I want to validate a textbox, currently im using this:

<script type="text/javascript">
function validate_form (){
	var Motor_Frame = document.getElementById('Motor_Frame');
	if(Motor_Frame.value == ""){
	alert("Please fill in Motor Frame. If no motor please fill in with N/M")
	document.forms[0].Motor_Frame.focus();  
 return false;}
 return true;
}
</script>

but i want it to check a few values the textbox is allowed:

1. if its blank display an error
2. if it doesnt equal a/p, n/r, n/q or a value between 1 and 500 then display an error.

From "if it doesnt equal a/p, n/r, n/q or a value between 1 and 500 then display an error" conditon, it seems that you only need a/p, n/r, n/q
values in textbox, if this is true then you ONLY need following condition:

if(Motor_Frame.value != "a/p" || Motor_Frame.value != "n/r" || Motor_Frame.value != "n/q")

yes thats true but it could also be a value between 1 and 500 as well....

if( (Motor_Frame.value != "a/p" || Motor_Frame.value != "n/r" || Motor_Frame.value != "n/q") || (parseInt(Motor_Frame.value) < 0 && parseInt(Motor_Frame.value) > 500)

i now have this:

<script type="text/javascript">
function validate_form (){
	var Motor_Frame = document.getElementById('Motor_Frame');
if( (Motor_Frame.value != "a/p" || Motor_Frame.value != "n/r" || Motor_Frame.value != "n/q") || (parseInt(Motor_Frame.value) < 0 && parseInt(Motor_Frame.value) > 500)){
		alert("Please fill in Motor Frame")
		document.forms[0].Motor_Frame.focus();  
 return false;}
 return true;
}
</script>

but my error comes up everytime, even if i eneter a number, n/q, n/r or a/p???

This is the working code, there were some mistakes in operators:

<script type="text/javascript">
function validate_form (){
	Motor_Frame = document.getElementById('Motor_Frame');
if((Motor_Frame.value.toString() != "a/p" && Motor_Frame.value.toString() != "n/r" && Motor_Frame.value != "n/q") && (parseInt(Motor_Frame.value) < 0 || parseInt(Motor_Frame.value) > 500))
{
	alert("Please fill in Motor Frame")
	document.forms[0].Motor_Frame.focus();  
 return false;
}
 return true;
}
</script>
<form>
<body>

<input type="text" id ="Motor_Frame" />
<input type="button" value ="Click" onclick="javascript:validate_form();" />
</body>
</form>

i can now enter anything or leave it blank and it will still save......

<script type="text/javascript">
function validate_form (){
	Motor_Frame = document.getElementById('Motor_Frame');
if((Motor_Frame.value.toString() != "a/p" && Motor_Frame.value.toString() != "n/r" && Motor_Frame.value != "n/q") )
{

  if (isInteger(Motor_Frame.value)==false  || parseInt(Motor_Frame.value) < 0 || parseInt(Motor_Frame.value) > 500 )
  {
	alert("Please fill in Motor Frame")
	document.forms[0].Motor_Frame.focus();  
        return false;
   }
}
 return true;
}


var reInteger = /^\d+$/; 
 function isInteger (s) {    
     return reInteger.test(s) 
  } 
 
</script>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.