473,467 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Validate input text field and clear if invalid

92 New Member
I have this javascript validation code that I want to be able to clear an input text box when the inputed value is invalid.

Note: this is a reusable validation code.
Expand|Select|Wrap|Line Numbers
  1. function isChar(form1)
  2. {
  3.     var letters = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZabcdefghijklmnñopqrstuvwxyz";
  4.     var strChar;
  5.     var dig = true;
  6.  
  7.     if (form1.length == 0) return false;
  8.  
  9.     for (a = 0; a < form1.length && dig == true; a++)
  10.     {
  11.         strChar = form1.charAt(a);
  12.         if (letters.indexOf(strChar) == -1)
  13.         {
  14.             dig=false;
  15.             alert("Invalid Input! Try Again.");
  16.             this.focus();
  17.         }
  18.         return dig;
  19.     }
  20. }
Replies are very much appreciated. Thanks.
May 21 '08 #1
7 4904
mrhoo
428 Contributor
Use a regular expression to test the whole input value at once-

Expand|Select|Wrap|Line Numbers
  1. function isChar(field){
  2.     if(/^[a-zñ]+$/i.test(field.value)) return true;
  3.     field.value= '';
  4.     alert("Invalid Input! Try Again.");
  5.  
  6.     setTimeout(function(){field.focus()},10);
  7. // you need the timeout, or the alert steals the focus
  8.  
  9.     return false;
  10. }
May 21 '08 #2
hsriat
1,654 Recognized Expert Top Contributor
Or use /^[a-zñÑ]+$/i in line 2 in the code by mrhoo, just in case it misses Ñ.

Not sure, but may be it won't consider Ñ as upper case of ñ.
May 21 '08 #3
mrhoo
428 Contributor
Not sure, but may be it won't consider Ñ as upper case of ñ
It seems to work, but it may depend on the character set in use- probably best to use hsriat's version.
May 21 '08 #4
backups2007
92 New Member
Use a regular expression to test the whole input value at once-

Expand|Select|Wrap|Line Numbers
  1. function isChar(field){
  2.     if(/^[a-zñ]+$/i.test(field.value)) return true;
  3.     field.value= '';
  4.     alert("Invalid Input! Try Again.");
  5.  
  6.     setTimeout(function(){field.focus()},10);
  7. // you need the timeout, or the alert steals the focus
  8.  
  9.     return false;
  10. }
this code does seem like it is only for 1 field. the code that I posted is a reusable code. how can your code be converted so that it can be applicable to any textbox?
May 23 '08 #5
hsriat
1,654 Recognized Expert Top Contributor
this code does seem like it is only for 1 field. the code that I posted is a reusable code. how can your code be converted so that it can be applicable to any textbox?
What do you exactly mean here by Reusable? How can say a code is reusable or not considering a small snippet of code?

Well, if you want this to validate all the inputs of a form, then make another function and call this isChar function in that, for each input of the form. Got it?
Expand|Select|Wrap|Line Numbers
  1. function isChar(field) {
  2.     if(/^[a-zñÑ]+$/i.test(field.value)) return true;
  3.     return false;
  4. }
And there is no need to do settimeout for focus. Alert won't steal anything. He's not a thief. :D
Just replace it with with field.focus()
May 23 '08 #6
backups2007
92 New Member
What do you exactly mean here by Reusable? How can say a code is reusable or not considering a small snippet of code?

Well, if you want this to validate all the inputs of a form, then make another function and call this isChar function in that, for each input of the form. Got it?
Expand|Select|Wrap|Line Numbers
  1. function isChar(field) {
  2.     if(/^[a-zñÑ]+$/i.test(field.value)) return true;
  3.     return false;
  4. }
And there is no need to do settimeout for focus. Alert won't steal anything. He's not a thief. :D
Just replace it with with field.focus()
I want to use the code to validate all inputs of a form and be able to reuse it in other forms.
May 26 '08 #7
hsriat
1,654 Recognized Expert Top Contributor
I want to use the code to validate all inputs of a form and be able to reuse it in other forms.
so it is... just call it in a loop for all the inputs of a form.
Expand|Select|Wrap|Line Numbers
  1. function validateForm(f) {
  2.     var ele = document.forms[f].elements;
  3.     var allValid = true;
  4.     for (var i in ele)
  5.     if (!isChar(ele[i]) && ele[i].type!="radio" && ele[i].type!="checkbox") {
  6.         allValid = false;
  7.         ele[i].style.borderColor = "red";
  8.     }
  9.     return allValid;
  10. }
May 26 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Mike | last post by:
I'm using a script provided by e-mailanywhere, it's a little too big for me. There's 1 text field and 1 password field in a form. OnSubmit, I would like both fields to be validated to look for...
5
by: Red | last post by:
Hi, I'm not very familiar with Javascript. I usually leave that kind of stuff up to Dreamweaver, but i'm starting to need a little more than it can offer. I have an asp page which creates a...
2
by: Meredith | last post by:
I am using a script to validate a form using the presence of a value in one field and determine if there is a value in one of two fields. It is an either/or situation. If the date rcvd field is...
1
by: John_H | last post by:
Re: ASP.NET 2.0 I would like suggestions or code examples on how to collect a variable length list of input data (item# & item quantity specifically). I thought that I could accomplish this...
11
by: jjbutera | last post by:
I know how to use the ErrorProvider in my winforms..or do I? I validate the values and set the ErrorProvider in the validating event. If not valid, I set e.Cancel = True. I clear the ErrorProvider...
11
by: TokyoJ | last post by:
I run a small camp in Alaska for kids and my director is asking for a web form. Could someone please have a look and offer some advice on where I'm making mistake(s)? I'm using the RegExp function...
3
by: shyamg | last post by:
hi all, This javascript is working IE but not working in FIreFox, validating text fields. var dealerid = new keybEdit('abcdefghijklmnopqurstuvwxyz01234567890 ','Alpha-numeric input only.'); ...
1
by: SkipNRun | last post by:
I am a novice when comes to JavaScript, AJAX. I am working on a form, which will allow users to update their contact information. In order to make the form flexible, I need to use pull down list. ...
1
by: mbarnhizer | last post by:
Hello All, Trying to figure out how to validate a series of questions on an online test. I am thinking that VB or Javascript is the best route, but your input may make a difference. The site i...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.