473,383 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Easiest Form Validation With Javascript?

chunk1978
224 100+
hi everyone,

this may seem like a lazy post, but it's not... i've been searching for a while and everything i've read seems different than others (and none of it works either, for me anyway)... in my form i already have a CheckForm Function under Javascript to display an alert if the fields were left blank, but i would also like to add to this existing code, in the same function (if possible), to alert if fields are containing illegal characters.

1) for the Name, i would like to allow only letters.
2) for the Email, i would like to allow only an email address with "@" and "."
3) for the ZipCode, i would like to allow only numbers.
4) for the Telephone, i would like to allow only numbers along with "(" and ")" and "-" and "+"


Expand|Select|Wrap|Line Numbers
  1.  
  2. function checkform ( form )
  3.     {
  4.     if (form.name.value == "") {
  5.     alert( "Please enter your NAME." );
  6.     form.name.focus();
  7.     return false ;}
  8.  
  9.       if (form.email.value == "") {
  10.     alert( "Please enter your EMAIL ADDRESS." );
  11.     form.email.focus();
  12.     return false ;}
  13.  
  14.     if (form.postalzip.value == "") {
  15.     alert( "Please enter your ZIP CODE." );
  16.     form.postalzip.focus();
  17.     return false ;}
  18.  
  19.     if (form.telephone.value == "") {
  20.     alert( "Please enter your TELEPHONE NUMBER." );
  21.     form.telephone.focus();
  22.     return false ;}
  23.  
  24.     return true ;
  25.     }
  26.  
  27.  
also, is it a security risk to allow "(" and ")" and "-" and "+" with the telephone field, or any other field? does allow these specific characters allow for malicious code to be entered that would kill the server when the form is submitted for processing?

thanks
Jan 17 '07 #1
7 2324
acoder
16,027 Expert Mod 8TB
1) for the Name, i would like to allow only letters.
2) for the Email, i would like to allow only an email address with "@" and "."
3) for the ZipCode, i would like to allow only numbers.
4) for the Telephone, i would like to allow only numbers along with "(" and ")" and "-" and "+"
If you know how to use regular expressions, you can match the strings with a regular expression, e.g. numbers only could be
Expand|Select|Wrap|Line Numbers
  1. /^\d+$/
See here for more info.

For the name, try using an else after the code for checking if name is empty, e.g.
Expand|Select|Wrap|Line Numbers
  1. if (form.name.value == "") {
  2.  alert( "Please enter your NAME." );
  3.  form.name.focus();
  4.  return false;
  5. } else if (!form.name.value.match(regexp) { // regexp is your regular expression for letters only
  6. // error message
  7.  
Jan 17 '07 #2
AricC
1,892 Expert 1GB
1) for the Name, i would like to allow only letters.
2) for the Email, i would like to allow only an email address with "@" and "."
3) for the ZipCode, i would like to allow only numbers.
4) for the Telephone, i would like to allow only numbers along with "(" and ")" and "-" and "+"
You should reconsider after you get the hang of doing some extra validation. For name you should only allow letters + spaces, for email you should allow more than just @ and "." what about "-" etc. For the email validation maybe a few others you should do a google search there are plenty of examples of prewritten JS to validate email addy's (no sense re-inventing the wheel)
Jan 17 '07 #3
chunk1978
224 100+
i've never used regular expressions before, and after spending 3+ trying to make it work i've decided that maybe it's not the right choice for me... my learning curve isn't so hot i guess ;-)...

i was thinking of something more literal, something my poor brain could manage to understand... something like this (if it's possible):

Expand|Select|Wrap|Line Numbers
  1.  
  2. var ATLEASTTHESECHARACTERS = "@", ".";
  3. if (form.email.value == "" || form.email.value !=('ATLEASTTHESECHARACTERS'));{
  4.     alert( "Please enter a valid EMAIL ADDRESS." );
  5.     form.email.focus();
  6.     return false ;}
  7.  
  8. var ATLEASTTHESECHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  9. if (form.name.value == "" || form.name.value !=('ATLEASTTHESECHARACTERS')) {
  10.     alert( "Please enter a valid EMAIL ADDRESS." );
  11.     form.email.focus();
  12.     return false ;}
  13.  
  14.  
is there anything super simple like that which i could implement into my existing code?
Jan 17 '07 #4
r035198x
13,262 8TB
i've never used regular expressions before, and after spending 3+ trying to make it work i've decided that maybe it's not the right choice for me... my learning curve isn't so hot i guess ;-)...

i was thinking of something more literal, something my poor brain could manage to understand... something like this (if it's possible):

Expand|Select|Wrap|Line Numbers
  1.  
  2. var ATLEASTTHESECHARACTERS = "@", ".";
  3. if (form.email.value == "" || form.email.value !=('ATLEASTTHESECHARACTERS'));{
  4.     alert( "Please enter a valid EMAIL ADDRESS." );
  5.     form.email.focus();
  6.     return false ;}
  7.  
  8. var ATLEASTTHESECHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  9. if (form.name.value == "" || form.name.value !=('ATLEASTTHESECHARACTERS')) {
  10.     alert( "Please enter a valid EMAIL ADDRESS." );
  11.     form.email.focus();
  12.     return false ;}
  13.  
  14.  
is there anything super simple like that which i could implement into my existing code?
Get a tutorial on regular expressions and read it. The rest of your Javascript validations will become an overly simplified task after that.
Jan 18 '07 #5
chunk1978
224 100+
Get a tutorial on regular expressions and read it. The rest of your Javascript validations will become an overly simplified task after that.
ok so i've done a lot of homework on regular expressions... but they seem temperamental with my browser (Safari 2.0.4)... sometimes they work, sometimes they don't (??)... so there must be a flaw in the code... this is what i have:

Expand|Select|Wrap|Line Numbers
  1.  
  2. function checkform ( form )
  3.     {
  4.     if (!/[a-zA-Z]+$/.test(form.name.value)) {
  5.     alert( "Please enter a valid NAME." );
  6.     form.name.focus();
  7.     return false ;}
  8.  
  9.     if (!/[\w\s]+$/.test(form.addressline1.value)) {
  10.     alert( "Please enter a valid ADDRESS." );
  11.     form.addressline1.focus();
  12.     return false ;}
  13.  
  14.     if (!/[a-zA-Z]+$/.test(form.city.value)) {
  15.     alert( "Please enter a valid CITY." );
  16.     form.city.focus();
  17.     return false ;}
  18.  
  19.     if (!/[a-zA-Z]+$/.test(form.provincestate.value)) {
  20.     alert( "Please enter a valid PROVINCE/STATE." );
  21.     form.provincestate.focus();
  22.     return false ;}
  23.  
  24.     if (!/[\w\s]+$/.test(form.postalzip.value)) {
  25.     alert( "Please enter a valid POSTAL/ZIP CODE." );
  26.     form.postalzip.focus();
  27.     return false ;}
  28.  
  29.  
Jan 18 '07 #6
chunk1978
224 100+
ok so i've done a lot of homework on regular expressions... but they seem temperamental with my browser (Safari 2.0.4)... sometimes they work, sometimes they don't (??)... so there must be a flaw in the code... this is what i have:

Expand|Select|Wrap|Line Numbers
  1.  
  2. function checkform ( form )
  3.     {
  4.     if (!/[a-zA-Z]+$/.test(form.name.value)) {
  5.     alert( "Please enter a valid NAME." );
  6.     form.name.focus();
  7.     return false ;}
  8.  
  9.     if (!/[\w\s]+$/.test(form.addressline1.value)) {
  10.     alert( "Please enter a valid ADDRESS." );
  11.     form.addressline1.focus();
  12.     return false ;}
  13.  
  14.     if (!/[a-zA-Z]+$/.test(form.city.value)) {
  15.     alert( "Please enter a valid CITY." );
  16.     form.city.focus();
  17.     return false ;}
  18.  
  19.     if (!/[a-zA-Z]+$/.test(form.provincestate.value)) {
  20.     alert( "Please enter a valid PROVINCE/STATE." );
  21.     form.provincestate.focus();
  22.     return false ;}
  23.  
  24.     if (!/[\w\s]+$/.test(form.postalzip.value)) {
  25.     alert( "Please enter a valid POSTAL/ZIP CODE." );
  26.     form.postalzip.focus();
  27.     return false ;}
  28.  
  29.  
so i managed to figure out what exactly is happening... based on the above code, in the "name" text area i can enter a valid name (IE "Steve Jobs"), since my regular expressions is only suppose to allow for alphabetic strings.however, i can also enter "1Steve 2Jobs" as well as "@$^#&Steve 8374Jobs"

so how do i fix this coding error?
Jan 18 '07 #7
r035198x
13,262 8TB
so i managed to figure out what exactly is happening... based on the above code, in the "name" text area i can enter a valid name (IE "Steve Jobs"), since my regular expressions is only suppose to allow for alphabetic strings.however, i can also enter "1Steve 2Jobs" as well as "@$^#&Steve 8374Jobs"

so how do i fix this coding error?
Perhaps your expressions are wrong. Try these onKeyPress of the input box

[HTML]function lettersOnly(e)
{
var keynum
var keychar
var numcheck
if(window.event) // IE
{
keynum = e.keyCode
}
else if(e.which) // Netscape/Firefox/Opera
{
keynum = e.which
}
keychar = String.fromCharCode(keynum)
numcheck = /\d/
return !numcheck.test(keychar)
}
function numeralsOnly(evt) { //integers only
evt = (evt) ? evt : event;
var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
((evt.which) ? evt.which : 0));
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
return true;
}

function floatingPoint(evt) { //numerical
evt = (evt) ? evt : event;
var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
((evt.which) ? evt.which : 0));
if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46) {
return false;
}
return true;
}[/HTML]
Jan 18 '07 #8

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

Similar topics

5
by: TG | last post by:
Dear PHP Group, I have two forms that are used to collect user information. The first one takes user inputted values such as fullname, city, address etc. I want these values to display in the...
6
by: Darren | last post by:
I have a form that has 10 fields on it. I have made all of them "Required". I also am using vb if statements to decide whether or not each field should be on the page. I am using the vb to...
2
by: iam247 | last post by:
Hi I have an ASP form which only includes an option list. The list is dynamically created but includes a default value, which is an instruction "Select a group". The code is shown at bottom....
16
by: Hosh | last post by:
I have a form on a webpage and want to use JavaScript validation for the form fields. I have searched the web for form validation scripts and have come up with scripts that only validate...
9
by: julie.siebel | last post by:
Hello all! As embarrassing as it is to admit this, I've been designing db driven websites using javascript and vbscript for about 6-7 years now, and I am *horrible* at form validation. To be...
27
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it...
11
by: Rik | last post by:
Hello guys, now that I'm that I'm working on my first major 'open' forms (with uncontrolled users I mean, not a secure backend-interface), I'd like to add a lot of possibilities to check wether...
17
by: Petyr David | last post by:
Just looking for the simplest. right now my perl script returns an error messge to the user if the date string is invalid. would like to do this before accessing the server. TX
3
uranuskid
by: uranuskid | last post by:
Hey folks, I was going to include a contact form on my website. Well, in the first place that seemed an easy thing to do with a form that prompts a PHP file validating the input vaiables and using...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.