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;
	}


}

Recommended Answers

All 2 Replies

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

Use following expression

+[^\s]

.+ means any character

\s means space character

[^\s] means except space character

commented: useful +5
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.