• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Javascript help

Status
Not open for further replies.

Zapages

Member
Hi guys,

I was wondering how do I enable the Backspace key, and the arrow keys on the keyboard so the user can scroll and fix their mistakes.

Currently I am using this to only allow users to enter a specific alphabet characters inside the textarea:

Code:
<script type="text/javascript">
var allow_backspace = true;
var arrValidChars=new Array("A", "C", "G", "N", "T", "a", "c", "g", "n", "t");
var arrSpecialChars = new Array(0, 2, 6, 13, 19, 26, 28, 32, 39, 45);
function VerifyCharacters(event, objInput) {
	var strText = objInput.value;
	var keyCode = event.keyCode||event.charCode;
	if (InArray(arrSpecialChars, keyCode) >= 0)
		return true;
	var strChar = String.fromCharCode(keyCode);
	return (InArray(arrValidChars, strChar)>=0);
}

function InArray(arr, key) {
	for (var i=0; i<arr.length; i++) {
		if (arr[i] == key)
			return i;
	}
	return -1;
}
</script>

and

this is my recall for the script in the textarea:

Code:
<textarea name="Sequence" cols="60" rows = "10"  onkeypress= "return VerifyCharacters(event, this)"> </textarea>
 

Wads

Banned
I assume you are talking about like having multiple text fields (like a phone number) and say they make a mistake in the 1st box and it moves to the 2nd box they can hit bs and get back to the 1st box?
 

Zapages

Member
Wads said:
I assume you are talking about like having multiple text fields (like a phone number) and say they make a mistake in the 1st box and it moves to the 2nd box they can hit bs and get back to the 1st box?

Hmm... I was thinking just in one textbox...So they can just edit the allowed characters out. Nothing too crazy yet. :)
 

Prine

Banned
Id maybe set this up so all valid characters entered are stored in a variable then pushed into the text area element, any invalid characters would flag a message. Allows you to screen the text input upon keyup event, and you dont have to explicitly target each keycode (like backspace, arrowkeys, shift etc ).

Hope that made sense. And use Jquery for the love of god!
 

Crow

Member
Zapages said:
Hi guys,

I was wondering how do I enable the Backspace key, and the arrow keys on the keyboard so the user can scroll and fix their mistakes.

Currently I am using this to only allow users to enter a specific alphabet characters inside the textarea:

Code:
<script type="text/javascript">
var allow_backspace = true;
var arrValidChars=new Array("A", "C", "G", "N", "T", "a", "c", "g", "n", "t");
var arrSpecialChars = new Array(0, 2, 6, 13, 19, 26, 28, 32, 39, 45);
function VerifyCharacters(event, objInput) {
	var strText = objInput.value;
	var keyCode = event.keyCode||event.charCode;
	if (InArray(arrSpecialChars, keyCode) >= 0)
		return true;
	var strChar = String.fromCharCode(keyCode);
	return (InArray(arrValidChars, strChar)>=0);
}

function InArray(arr, key) {
	for (var i=0; i<arr.length; i++) {
		if (arr[i] == key)
			return i;
	}
	return -1;
}
</script>

and

this is my recall for the script in the textarea:

Code:
<textarea name="Sequence" cols="60" rows = "10"  onkeypress= "return VerifyCharacters(event, this)"> </textarea>


Hmm, that seems to be a little more complicated than it needs to be.

EDIT: forgot about your backspace issue, revised the code to allow it.

Try this:

javascript
Code:
function VerifyCharacters2(event){

	var code = event.keyCode?event.keyCode:event.charCode;
	
	if(code == 8 || code == 37 || code == 39) return true; //allow backspace, arrow keys
	
	var character = String.fromCharCode(code);
	return /[acgntACGNT]/.test(character);
}


html
Code:
<input type="text" onkeypress="return VerifyCharacters2(event)" />
 

Zapages

Member
^Thank you. :) I have one small question, what does the code == 8, etc suppose to indicate? I am just wondering?
 

charsace

Member
Do you need to do what you are doing with an array? I don't know java, but couldn't you use an if not statement?

if !( variable || variable || variable...etc
 
Status
Not open for further replies.
Top Bottom