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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Now I'm Having a problem evaluating these 2 conditions

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

 

You're over thinking this. Just check if it is 1:

if(x != 1)

 

Though I'd probably do a more strict check myself:

 

if(x !== "1")

Edited by haku
Link to comment
Share on other sites

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+$/))

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.