473,396 Members | 1,683 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,396 software developers and data experts.

help with javascript validation

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.registrationform.username
var re = /^\s{1,}$/g;

if ((username.value==null) || (username.value=="") || (username.length=="") || (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=document.registrationform.postcode
var postcodestripped = postcode.value.replace(/[\(\)\.\-\ ]/g, '');

if (isNaN(parseInt(postcodestripped)))
{
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 pcontactnumbertf=document.registrationform.pcontac tnumbertf

var pcontactnumbertfnumber = document.registrationform.pcontactnumbertf.value;
var checkfornumber = parseFloat(pcontactnumbertfnumber);

if ( isNaN( checkfornumber ) )
{
alert( "Please enter a numeric number" );
document.registrationform.pcontactnumbertf.focus() ;
document.registrationform.pcontactnumbertf.select( );
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 1430
hsriat
1,654 Expert 1GB
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
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,...
3
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...
2
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...
4
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...
1
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...
2
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"...
36
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...
18
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...
2
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...
6
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...
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: 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
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...
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
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,...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.