|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Disable characters with javascript in form field
Does anyone know the code to disable everything except "a-z" and "_" in a form field?
|
|
#2
|
||||
|
||||
|
Here's a simple javascript example using Regular Expressions:
Code:
<script>
function validate() {
var box = document.frm1.box;
re=/^[a-zA-Z\_]*$/;
if(!re.exec(box.value)) {
alert("Invalid Entry:\nOnly Alphabetic characters or Underscore allowed!");
box.focus();
return;
}//end name if
}
</script>
<form name="frm1">
<input type="text" name="box" onchange="validate();">
</form>
I made the method call when the text is changed. Feel free to play around with onkeypress() or onblur() Some good resources for Javascript Regular Expressions: http://www.sitepoint.com/article/286 http://devedge.netscape.com/library...ide/regexp.html http://www.webreference.com/js/column5/
__________________
Daryl's Homepage | My Blogroll | My Profile | Firefox supporter! DevArticles Forum Moderator "The net is a waste of time, and that's exactly what's right about it." -- William Gibson |
|
#3
|
|||
|
|||
|
THX, now can you help me modify the code to allow numbers too?
I tried PHP Code:
but it doesnt work if i put in stuff like @$%^%&@!* |
|
#4
|
||||
|
||||
|
i haven't tested it, but try:
re=/^[0-9a-zA-Z\_]*$/; |
|
#5
|
|||
|
|||
|
yup the number worked but i found out that it still lets the form continue on. Is there a small change that will empty that form field if the message pops up?
|
|
#6
|
||||
|
||||
|
Instead of doing a submit button (like i assume you have) you could make a regular button and make the onclick event call validate()
Wait, even better... make a seperate function like validateForm() which calls validate() then submits the form. For example: Code:
<script>
...
function validateForm() {
if (validate()) {
document.frm1.submit()
}
}
...
</script>
Modify the code I gave you in an earlier post... where it says "return;", have it say return false... then add a return true to the end of the validate() function (but outside the if statement) I hope this makes sense to you... There's great examples of Javascript Form Validation on the internet... [check some of the links I posted early] |
|
#7
|
||||
|
||||
|
I just noticed, an article was posted on Devshed today:
Validation with Javascript http://www.devshed.com/Client_Side/...ript/page1.html |
|
#8
|
|||
|
|||
|
Hey Mate!. The code is quite nice. I'd suggest however to do the following:
1) place a parameter on the function like validate(txt) and after the alert part, set txt.value = "" (if you want, you can place a document.form...submit() if no error occurs) 2) call the function like this: <input type="text" name="..." onChange="validate(this)"> Hope this works... Anibal Quote:
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > Disable characters with javascript in form field |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|