0

Below is my javascript validation file which i hv imported in my php form. the problem is the kind of pattern(or function in the javascript file below) i m using in case of name address and username they r validating correctly but they r not including the spaces in the text field. for eg : in case of name field i have given the pattern which accepts only alphabets i.e ((/^[a-zA-Z]+$/) == -1) so it is validating according to it but not considering the spaces and i want to consider the spaces too.
more explanation below by an example.
Piyushjain :-> Valid (no error)
but
Piyush Jain :-> Invalid (error: enter alphabets only) i.e its not considering space as an alphabet resulting in type mismatch and error)
Can someone edit the pattern and include validation for spaces too so that it will nt show an error. help me asap.

function validate()
{
	var n=document.getElementById('name').value;
	
	validatename(n);
	
	
	
	function validatename(fld)
	{
	if(fld.search(/^[a-zA-Z]+$/) == -1)
                 {
					alert("Enter name in letters only");
					document.getElementById('name').focus();
					return false;
				}
return true;
	}
	
	
	var b=document.getElementById('address').value;
	
	validateaddress(b);
	
	
	
	function validateaddress(fld)
	{
	if(fld.search(/^[0-9a-zA-Z]+$/) == -1)
                 {
					alert("Enter valid Address in alphanumeric form only");
					document.getElementById('address').focus();
					return false;
				}
return true;
	}
	
	var a=document.getElementById('age').value;
	
	validateage(a);
	
	
	
	function validateage(fld)
	{
	if(fld.search(/^[0-9]+$/) == -1)
                 {
					alert("Enter age in numerals only");
					document.getElementById('age').focus();
					return false;
				}
return true;
	}
	
	var e=document.getElementById('emailid').value;
	
	validateemail(e);
	
	
	
	function validateemail(fld)
	{
	if(fld.search(/^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/) == -1)
                 {
					alert("Enter valid Email Address");
					document.getElementById('emailid').focus();
					return false;
				}
return true;
	}
	
	var f=document.getElementById('username').value;
	
	validateusername(f);
	
	function validateusername(fld) {
    
if(fld.search(/^[A-Za-z0-9_]{3,20}$/)== -1)                
{
					alert("Enter valid Username");
					document.getElementById('username').focus();
					return false;
				}
return true;
	}


	
	var g=document.getElementById('password').value;
	
	validatepassword(g);


function validatepassword(fld) 

{
	if(fld.search(/^[A-Za-z0-9!@#$%^&*()_]{6,20}$/)== -1)                 
	{
					alert("Enter valid Password");
					document.getElementById('password').focus();
					return false;
				}
return true;
	}


}
3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by divyakrishnan
0

you can validate spaces also like this :

var invalid = " "; // Invalid character is a space
// check for spaces
if (document.myForm.password.value.indexOf(invalid) > -1) {
alert("Sorry, spaces are not allowed.");
return false;

and also check this link :
http://www.dreamincode.net/code/snippet68.htm

Edited by Shanti C: n/a

1

Use following expression

+[^\s]

.+ means any character

\s means space character

[^\s] means except space character

Edited by divyakrishnan: n/a

Votes + Comments
useful
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.