-
Jul 15th, 2011, 04:24 PM
#1
Thread Starter
New Member
javascript validation
Hi all.
I have a slite problem with javascript
Lets say, i have variable, that are string
Code:
var = "1234567890abced"
And i need to check on click event, for textbox, if the pressed key matches some of the chars inside our string variable.
Can someone help me out at this?
-
Jul 16th, 2011, 10:21 AM
#2
Addicted Member
Re: javascript validation
Hello
if you want to check only one char I prefer this way more than using regExp
HTML Code:
<script type="text/javascript">
var fineChars = '123456abcdef';
function checkChar(inputField)
{
var pressedChar = inputField.value.substring(inputField.value.length - 1, inputField.value.length);
if (fineChars.indexOf(pressedChar) == -1) {
var newValue = inputField.value.substring(0, inputField.value.length - 1);
inputField.value = newValue;
}
}
</script>
<input type="text" onkeyup="checkChar(this)" />
regards
Feras Jobeir
-
Jul 16th, 2011, 10:24 AM
#3
Addicted Member
Re: javascript validation
Hello
If you want to check a char only, I prefer to try this more that using RegExp
HTML Code:
<script type="text/javascript">
var fineChars = '123456abcdef';
function checkChar(inputField)
{
var pressedChar = inputField.value.substring(inputField.value.length - 1, inputField.value.length);
if (fineChars.indexOf(pressedChar) == -1) {
var newValue = inputField.value.substring(0, inputField.value.length - 1);
inputField.value = newValue;
}
}
</script>
<input type="text" onkeyup="checkChar(this)" />
regards
Feras Jobeir
-
Jul 17th, 2011, 08:44 AM
#4
Thread Starter
New Member
Re: javascript validation
Tnx for your replys guys, but i managed to do this on my own..
This is how i did it:
Code:
onkeyup="input_res(this,'abC chars to be allowed')
function input_res(object,validation){
var result = "";
for (i=0; i < object.value.length; i++) {
x = object.value.charAt(i);
if (validation.indexOf(x,0) != -1) {
result += x;
}
}
object.value = result
}
Good luck to everyone
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|