0

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.

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

2
Contributors
7
Replies
8
Views
5 Years
Discussion Span
Last Post by sufyan2011
0

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")

Edited by sufyan2011: n/a

0

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)

0

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???

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

0

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>

Edited by sufyan2011: n/a

0
<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>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.