469,309 Members | 2,783 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,309 developers. It's quick & easy.

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 1305
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

Post your reply

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

Similar topics

13 posts views Thread by dave yan | last post: by
3 posts views Thread by Sol Linderstein | last post: by
4 posts views Thread by Adrienne | last post: by
36 posts views Thread by aljamala | last post: by
2 posts views Thread by Chad Lupkes | last post: by
6 posts views Thread by smk17 | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.