Jump to content

pass a form field value to javascript substring()


simora

Recommended Posts

I'm trying to pass a form field value to substring() to evaluate it.

my form field is like this;

 

Word here : <input type="text" name="word" value = " " onmouseout="functionones()">

 

 

I need to make sure that "word" is numeric only and that it begins with the number 1 If NOT, I want to show an alert and send them to another location.

 

I tried this, but its not working.

 

<script type="text/javascript">
function functionones()
 var f =(document.form1.word.value): 
 var x = f.substring(0,1); 

{

 if (var x >1)
 alert("Your word data is wrong");
 window.location = 'http://www.basy.biz'; 
}

//-->
</script>

 

If I hard code a value of "123343466" into var f and do a doc.write, I'll get 1

 

Appreciate your help.

Your variables are declared outside of the function for a start.

 

Why not just set the max length of the input field to 1? JS is client sided, so it's the same security .. If you need JS to deal with it on top of that, I would still keep the input's length limit just to make things easier for the client. Maybe not even bother reading the value from the field ..

 

Demo: http://xaotique.no-ip.org/tmp/40/

 

HTML

<form method="post" action="#">
<input type="text" maxlength="1" id="word" style="width: 15px; text-align: center;" />
<span id="result"></span>
</form>

 

Javascript

// Wait for window to load
window.addEventListener("load", function()
{
// Wait for keyup to trigger
window.document.querySelector("#word").addEventListener("keyup", function(event)
{
	// Check inputted value
	var input = event.which >= 65 && event.which <= 90 ? String.fromCharCode(event.keyCode) : null;

	// Check if key pressed is a-z (add more above)
	if (input != null)
	{
		// Set the input field to the character.
		this.value = input;
	}
	else
	{
		// Key pressed wasn't a-z
	}
}, false);
}, false);

Xaotique: THANKS !

 

 

I got this to work like this:

 

 

<script language="javascript">

function showAlert() {
var f =document.form1.word.value; 
var x = f.substring(0,1); 
if(x > 1){
alert(x +" is greater than 1 ");
window.location = "http://www.basy.com/";
} 
}
</script>

 

Now I'm Having a problem evaluating these 2 conditions

if(x > 1) || (!IsNumeric(x))

 

I need x to be 1 and Numeric. If there's a letter in the first position, this will catch it.

 

RE: Why not just set the max length of the input field to 1

.The field is used for multiple things. The # 1 at the beginning is my way to filter specific people.

A freebee for you. This will test if the first digit is one, and the rest of the characters after the one are also numeric. If the first digit is not 1, or any of the rest of the characters are not numeric, then it will return false.

if(f.match(/^1\d+$/))

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.