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.pcontactnumbertf 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.
here is Solution for your problem 1. use trim function function trim(str) { return str.replace(/^\s*|\s*$/g,""); } 2 . use checkName function to validate alpha numeric values function checkName(str,mes) { var x = str; var filter = /^([a-zA-Z\s0-9]*)$/; if (!filter.test(x)) { alert(mes); fld.focus() } else return false; return true; } 3,5 . user checknumber function to validate numbers function checkNumber(str,mes) { var x = str; var filter = /^([0-9]*)$/; if (!filter.test(x)) { alert(mes); fld.focus() } else return false; return true; } 4 user checkAlpha for only alphabets function checkAlpha(str,mes) { var x = str; var filter = /^([a-zA-Z]*)$/; if (!filter.test(x)) { alert(mes); fld.focus() } else return false; return true; }
hi, I see that you are trying to custom code your validation of your form. Wel i think I can hep you a bit to get a flying start. I have written a small javascript library that validated form client-side. With this library you do not need to code javascript to use it. You only need to code javascript if you want to do specific non basic checks in your forms. My library support languages, local and global display of error messages, masking and extendable using your own javascript methods. www dot wowww dot nl / wordpress /? p=102 <= Go here for a full explanation and source Kind regards, Marc