Click to See Complete Forum and Search --> : check input
jagguy
07-02-2007, 04:19 AM
Hi,
I want to check whether a textbox or password box has non char vales eg ' " & // or any other type . I just want 0-9 a-z chars before passig values to a script.
var fname=document.getElementById("file1").value;
submitOK="true"
if (fname.length ==0 || ( !fname.match(/\...
samanyolu
07-02-2007, 08:44 AM
match()
<script type="text/javascript">
function check() {
var el = document.getElementById('inputid');
var re = /[a-z0-9]/
if(el.length == 0 || !el.value.match(re)) {alert("not okey"); el.focus(); return false;}
}
</script>
<form action="index.php" method="post" onsubmit="return check()">
<input type="text" id="inputid">
<input type="submit" value="submit">
</form>
test()
<script type="text/javascript">
function check() {
var el = document.getElementById('inputid');
var re = /[a-z0-9]/
if(el.length == 0 || !re.test(el.value)) {alert("not okey"); el.focus(); return false;}
}
</script>
<form action="index.php" method="post" onsubmit="return check()">
<input type="text" id="inputid">
<input type="submit" value="submit">
</form>
var re = /[a-z0-9]{4,8}/
var re = /[a-z0-9]{from 4 character, to 8 character}/
<script type="text/javascript">
function check() {
var el = document.getElementById('inputid');
var re = /[a-z0-9]{4,8}/
if(!re.test(el.value)) {alert("not okey"); el.focus(); return false;}
}
</script>
<form action="index.php" method="post" onsubmit="return check()">
<input type="text" id="inputid">
<input type="submit" value="submit">
</form>
samanyolu
07-02-2007, 02:16 PM
There is an error in my code.
It will be
var re = /^[a-z0-9]$/g
or
var re = /^[a-z0-9]{4,8}$/g
jagguy
07-04-2007, 07:21 PM
Hi,
thanks for the effort but I cant get it to work.
I want the user to enter in either numbers or letters. Anything else I dont want but none of this code seems to do that?
JMRKER
07-04-2007, 09:15 PM
The regular expressions are a better way to go,
but if you need to just get a simplified alphanumeric checker,
try this:
<html>
<head>
<title>AlphaNumeric Check</title>
<script type="text/javascript">
var Alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
var Numeric = '0123456789';
function isAlphaNumeric(v) {
var flag = 0;
var c = '';
for (i=0; i<v.length; i++) {
c = v.charAt(i);
if ((Alpha+Numeric).indexOf(c) < 0) { flag = -1; }
}
return flag;
}
</script>
</head>
<body>
<input type="text" size="40" id="entry">
<button
onClick="if(isAlphaNumeric(document.getElementById('entry').value)<0){alert('Invalid');}else{alert('OK');}">
Check Entry</button>
</body>
</html>
jagguy
07-04-2007, 10:27 PM
what are the regular expressions because the code from samanylou didnt work.
JMRKER
07-04-2007, 11:12 PM
That was the 're = ...' and '... match(re)...' and 're.test(...' stuff in his/her post.
Regular Expressions (also known as 'regex') are very powerful tools.
See:
http://lawrence.ecorp.net/inet/samples/regexp-intro.php
http://www.javascriptkit.com/jsref/regexp.shtml
and a lot of others with a 'google search' or a search of this forum.
Once you get a handle on the regular expression, try their code again.
You should be getting an alert message with invalid entries on either the match() or test() examples. Don't try to use both in the same script initially.