problem in validation script for whitespaces

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
djdon11
Forum Commoner
Posts: 90
Joined: Wed Jun 20, 2007 5:03 pm

problem in validation script for whitespaces

Post by djdon11 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello friends ..

i have a problem in my script 

here is a snippet of javascript code which is for the checking of alphabetic fields only means only alphabates can be enter in the text fields .. but a problem is going on with this if user enters only alphabates then there should be no any error but users enters all the alphabates with white spaces then there is message coming only alphabtes are allowed , means this code is not considering white spaces as a alphabate so have a look on this code and give me idea what should i have to do for fixing my bug .............. here is the code : - >


[syntax="javascript"]



 case "alphabetic": 
        case "alpha": 
           { 
              var charpos = objValue.value.search("[^A-Za-z]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                  if(!strError || strError.length ==0) 
                { 
                  strError = objValue.name+": Only alphabetic characters allowed\n No Whitespaces "; 
                }//if                             
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 
                return false; 
              }//if 
              break; 
           }//alpha 


Please help me soon

Thanks


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
rturner
Forum Newbie
Posts: 24
Joined: Sun Nov 04, 2007 1:39 pm

Re: problem in validation script for whitespaces

Post by rturner »

djdon11 wrote:feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello friends ..

i have a problem in my script 

here is a snippet of javascript code which is for the checking of alphabetic fields only means only alphabates can be enter in the text fields .. but a problem is going on with this if user enters only alphabates then there should be no any error but users enters all the alphabates with white spaces then there is message coming only alphabtes are allowed , means this code is not considering white spaces as a alphabate so have a look on this code and give me idea what should i have to do for fixing my bug .............. here is the code : - >


[syntax="javascript"]



 case "alphabetic": 
        case "alpha": 
           { 
              var charpos = objValue.value.search("[^A-Za-z ]");  //  <- see the space here....
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                  if(!strError || strError.length ==0) 
                { 
                  strError = objValue.name+": Only alphabetic characters allowed\n No Whitespaces "; 
                }//if                             
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 
                return false; 
              }//if 
              break; 
           }//alpha 


Please help me soon

Thanks


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color][/quote]

Just add a space to allow whitespace.  You may want to allow punctuation like comma and period too!

[syntax="javascript"]

var charpos = objValue.value.search("[^A-Za-z ]");  // <- see the space here
[/syntax]
devendra-m
Forum Contributor
Posts: 111
Joined: Wed Sep 12, 2007 3:16 am

Post by devendra-m »

\s is meant for white spaces in regular expression.


[^A-Za-z\s]
Post Reply