473,788 Members | 2,719 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help with javascript validation

54 New Member
i have a registration page where a user fills up a form such as username, first name, phone number etc.

i need to do a validation for the following fields. any help will be greatly appreciated.

1. for a text field if for example a user hits the space bar and then types the information with the validation i have the code ignores those initial spaces but actually these are spaces i need to insert these values in a database so these spaces may cause an error.

how to validate these initial spaces and tell the user not to use spaces at the beginning of a textfield

my code at present is =

var username = document.regist rationform.user name
var re = /^\s{1,}$/g;

if ((username.valu e==null) || (username.value =="") || (username.lengt h=="") || (username.value .search(re))> -1)
{
alert("Please Enter a User Name")
username.value= ""
username.focus( )
return false
}

2. validation of special characters such as = !"£$%_^&*(). In general all the various special characters

my code at present is =

var postcode=docume nt.registration form.postcode
var postcodestrippe d = postcode.value. replace(/[\(\)\.\-\ ]/g, '');

if (isNaN(parseInt (postcodestripp ed)))
{
alert( "The post code contains illegal characters" );
postcode.value= ""
postcode.focus( );
return false
}

3. validation of numbers = the code i have validates for numbers only if the first characters are letters but if there are letters in between the validation does not work. ex= ab12 validates however 12ab does not validate

var pcontactnumbert f=document.regi strationform.pc ontactnumbertf

var pcontactnumbert fnumber = document.regist rationform.pcon tactnumbertf.va lue;
var checkfornumber = parseFloat(pcon tactnumbertfnum ber);

if ( isNaN( checkfornumber ) )
{
alert( "Please enter a numeric number" );
document.regist rationform.pcon tactnumbertf.fo cus();
document.regist rationform.pcon tactnumbertf.se lect();
return false
}


4. validating for characters only and nothing else. ex= name and not name123


5. validating for numerics only and nothing else. ex= 123 and not 123name


for all the above 5 questions i would really appreciate if someone can provide the appropriate code that needs to be written in order to validate each individual point i have mentioned.

thanks a lot.
Feb 25 '08 #1
1 1453
hsriat
1,654 Recognized Expert Top Contributor
Wrap your code in a function, say validateForm(), and call it on onsubmit event of the form.
Feb 25 '08 #2

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

Similar topics

13
5854
by: dave yan | last post by:
hi, i have some forms which use javascript for data validation, e.g., checking to make sure all required fields are completed, checking that data falls within valid ranges for certain fields, etc. if an error occurs, i'm sending the user a message via the alert window. the form is then not submitted until the errors are corrected. however, i've discovered that some users using the aol browser are
3
1889
by: Sol Linderstein | last post by:
Hi, I'm writing a CGI application and here's what I'm stuck with. There are some text boxes in an html form that I want to validate. The validation needs to happen on the server side because the data to validate against is inside a mysql database residing on the server. I want to present the user with a popup only if the validation fails. Ideally the user doesn't even know the validation is happening unless it fails. He just moves from...
2
1356
by: francisco lopez | last post by:
Yesterday I had a problem with a javascript to validate my form, but you helped my out yesterday and it works now perfectly!!! so thank you!!! the problem I have now is the following: I put my form now on the server and I changed following lines: I changed: form action="mailto:fran@gmx.net"
4
2007
by: Adrienne | last post by:
I am the first to admit that I know bupkis about javascript, except that sometimes I need it to do something client side that I can't do server side. Anyway, here's my problem: <input type="text" name="ticket" id="ticket"> <input type="text" name="amount" id="amount"> My ASP script defaults ticket to be 30.00 and amount to be 600.00. What
1
1525
by: Paul | last post by:
Hi, I'm developing a search facility for my web app. In order for the search criteria to be valid the user must (a) enter some text in a text box and check and least one checkbox (of which there are two) and/or (b) select at least one item from a multiple select list box. I've written the javascript to do the client side validation which gets called on the click event of my search button. I'd like to implement server side validation...
2
4895
by: this one | last post by:
I have the following code <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script language="JavaScript" src="..\include\gen_validatorv2.js" type="text/javascript"></script> </head>
36
3111
by: aljamala | last post by:
Hi, I keep getting this warning on a page, but I do not know what the problem is...does anyone have an idea about what could be wrong? line 88 column 7 - Warning: missing </formbefore <td> it highlights this line: <form name="frmCurrency" action="" method="post"> Page source....
18
5809
by: Axel Dahmen | last post by:
Hi, trying to submit an ASPX form using the key (using IE6) the page is not submitted in my web project. Trying to debug the pages' JavaScript code I noticed that there's some ASP.NET client script code being executed having a flaw: function anonymous() { if (!ValidatedTextBoxOnKeyPress(event)) { event.cancelBubble = true; if (event.stopPropagation) event.stopPropagation(); return false; } }
2
1468
by: Chad Lupkes | last post by:
Hi everyone, I need help with a simple form validation. The form I'm using has been the target of some spammers, and I'm wondering what else I can do. I have a very simple validation, checking for blanks, and found an additional zipcode validation form here: http://javascript.internet.com/forms/val-zip-code.html I tried combining them, and it doesn't seem to be working. I'm not
6
1870
by: smk17 | last post by:
I've spent the last few minutes searching for this question and I found an answer, but it wasn't quite what the client wanted. I have a simple online form where the user needs to fill out five fields out of nine. The other four are already there and filled out for the user. When they hit submit, all data is sent to us. But, if they desire (for whatever reason) the user can possibly delete what is already there and fill in something...
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10373
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10177
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10118
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8995
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6750
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5403
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4074
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 we have to send another system
3
2897
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.