473,405 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

javascript validation error message

114 100+
I keep getting this error message after adding javascript form validation. If anyone goes to the page with the form below, they get the invite to debug

I have tried adding it to the onSubmit and Onclick but same problem. Any tips? thanks in advance


Line: 167
Error Expected '}'



this is the line causing the problem for some reason,


Expand|Select|Wrap|Line Numbers
  1.        <input type="Submit" onClick="return ValidateRegister()" value="Register Now" />
  2.                    <br /></td>
and this is the function

Expand|Select|Wrap|Line Numbers
  1.  
  2.   function ValidateRegister(){     
  3.  
  4. if (document.frm.email.value.length >0) {
  5.   i=document.frm.email.value.indexOf("@")
  6.   j=document.frm.email.value.indexOf(".",i)
  7.   k=document.frm.email.value.indexOf(",")
  8.   kk=document.frm.email.value.indexOf(" ")
  9.   jj=document.frm.email.value.lastIndexOf(".")+1
  10.   len=document.frm.email.value.length
  11.  
  12.   if ((i>0) && (j>(1+1)) && (k==-1) && (kk==-1) && (len-jj >=2) && (len-jj<=3)) {
  13.   }
  14.   else {
  15.    alert("Please enter a valid email address.\n" +
  16.   document.frm.email.value + " is invalid.");
  17.   document.frm.email.focus()
  18.   return false; }
  19.  
  20.  
  21.   var regExp = /^[A-Za-z][A-Za-z0-9_]*$/;
  22.    if(!regExp.test(frm.username.value))
  23.      alert("no special characters")
  24.    if(frm.username.value.length > 18 || frm.username.value.length < 5)
  25. alert("username should be between 5-18 characters")
  26.  for(i=0;i<names.length;i++) {
  27.  document.frm.username.focus()
  28.    return false; }
  29.  
  30.  
  31.   var regExp = /^[A-Za-z][A-Za-z0-9_]*$/;
  32.    if(!regExp.test(frm.password.value))
  33.      alert("no special characters")
  34.    if(frm.password.value.length > 18 || frm.password.value.length < 5)
  35. alert("password should be between 5-18 characters")
  36.  for(i=0;i<names.length;i++) {
  37.  document.frm.password.focus()
  38.    return false; }
  39.  
  40.   var alphaExp = /^[a-zA-Z]+$/;
  41.  if(document.frm.name.match(alphaExp)){
  42.   return true; }
  43.  else {
  44.   alert("letters only");
  45.  document.frm.name.focus();
  46.   return false; }
  47.  
  48.  
  49.        if(document.frm.name.value.length<5) {  
  50.    alert("minimum 5 characters for name.")
  51.    document.frm.name.focus()
  52.    return false;  }
  53.  
  54.    if(document.frm.name.value.length>30) {  
  55.    alert("maximum 30 characters for name")
  56.   document.frm.name.focus()
  57. return false;  
  58.    } 
  59.       return true; // 
  60.  
  61.     }
Apr 28 '07 #1
11 2226
pbmods
5,821 Expert 4TB
Have you tried embedding the validation directly into your elements? That makes it a lot easier when it comes time to validate the whole form.

http://www.thescripts.com/forum/post2532102-3.html

The problem may be that you're missing a semicolon at this line:
Expand|Select|Wrap|Line Numbers
  1. document.frm.password.focus()
(occurs a few times)

As a sidenote, why does a password have to be less than 18 characters long? Aren't you going to hash it anyway?
Apr 28 '07 #2
RKZA
8
Hi,
I had a quick scan through your code and I think for the first if statement you have not closed off with a curly bracket "}".

R.K.
Apr 28 '07 #3
karen987
114 100+
Thanks for replying.

Do you mean i should close the first part like this(below) or do you mean right at the end of what i posted above?

As for the password, there is no particular reason why i limited to 18 i just don't want a situation where people type in very long passwords. No other reason. Why is it bad practice?

Expand|Select|Wrap|Line Numbers
  1. if (document.frm.email.value.length >0) {
  2.   i=document.frm.email.value.indexOf("@")
  3.   j=document.frm.email.value.indexOf(".",i)
  4.   k=document.frm.email.value.indexOf(",")
  5.   kk=document.frm.email.value.indexOf(" ")
  6.   jj=document.frm.email.value.lastIndexOf(".")+1
  7.   len=document.frm.email.value.length
  8.  
  9. }

Please can anyone see what is wrong with line 6 in this code? There is an error in the 6th line.
Does anyone know what i need to change? im not sure if the 6th line should be
"for(i=0;i<document.frm.username.length;i++) {" instead of what is there now.


Expand|Select|Wrap|Line Numbers
  1. var regExp = /^[A-Za-z][A-Za-z0-9_]*$/; 
  2.    if(!regExp.test(frm.username.value)) 
  3.      alert("no special characters or spaces") 
  4.    if(frm.username.value.length > 18 || frm.username.value.length < 5) 
  5. alert("'Username' should be between 5-18 characters.") 
  6. for(i=0;i<names.length;i++) { 
  7. document.frm.username.focus() 
  8.             return false; } 
thnaks in advance
Apr 29 '07 #4
RKZA
8
Yip I think it be best to close the if statement after the variable "len".

For your line six you don't need the for statement to find set focus on the form field username, just keep the code: document.frm.username.focus(). This goes for the same in you passward section.

Now the only reason i can think of what you were tring to achieve with your for statement is to highlight all the characters in the text field. To do this you don't need a for statement just first document.frm.username.focus() then document.frm.username.select(); Not sure but i thought i would mention it.

R.K.
Apr 29 '07 #5
karen987
114 100+
Yip I think it be best to close the if statement after the variable "len".

For your line six you don't need the for statement to find set focus on the form field username, just keep the code: document.frm.username.focus(). This goes for the same in you passward section.

Now the only reason i can think of what you were tring to achieve with your for statement is to highlight all the characters in the text field. To do this you don't need a for statement just first document.frm.username.focus() then document.frm.username.select(); Not sure but i thought i would mention it.

R.K.
RKZA

actually the email statement works fine the curly bracket screwed it up. If you note, the following code is part of the statement i think. So the email works fine.

what's giving the problem is the "username" field, the second statement below in bold is the problem. I've tested both individually, and the first one works, but the 2nd one has a problem in that it stops there. ie. after the "username input" has been correctly entered, it should move onto validating whatever is wrong next, but it doesn't. When you click submit the focus goes on "username" but no error message or anything. I know that text entered in "username " is correct and should go through. So obviously the form can't submit, unless it finishes validation.

I'm sure it's something to do with the regex expression , by the way, i'm not html literate,

thanks for your help.

Expand|Select|Wrap|Line Numbers
  1.   if (document.frm.username.value.replace(/ /g, "") == "")  {
  2.             alert("Please enter desired username. Must be between 5-18 characters (no special characters).")
  3.             document.frm.username.focus() ;
  4.         return false; }
  5.  
  6.  
  7. var regExp = /^[A-Za-z][A-Za-z0-9_]*$/;
  8.    if(!regExp.test(document.frm.username.value))
  9.      alert("no special characters or spaces in 'username'")
  10.    if(document.frm.username.value.length > 18 || document.frm.username.value.length < 5)
  11. alert("'Username' should be between 5-18 characters.")
  12.  document.frm.username.focus() ;
  13.             return false; }

by the way, the complete code is below.

I want to add a statement that makes it mandatory for the user to choose an option from drop down called "country". it's a list of countries obviously. Any help appreciated.

Thanks a bunch


Expand|Select|Wrap|Line Numbers
  1.    // VALIDATE ADD
  2.     function ValidateUser(){
  3.  
  4.         if (document.frm.email.value.replace(/ /g, "") == "")  {
  5.             alert("Please enter your email address.")
  6.             document.frm.email.focus() ;
  7.         return false; }    
  8.  
  9.         if (document.frm.email.value.length >0) {
  10.      i=document.frm.email.value.indexOf("@")
  11.      j=document.frm.email.value.indexOf(".",i)
  12.      k=document.frm.email.value.indexOf(",")
  13.      kk=document.frm.email.value.indexOf(" ")
  14.      jj=document.frm.email.value.lastIndexOf(".")+1
  15.      len=document.frm.email.value.length     
  16.      if ((i>0) && (j>(1+1)) && (k==-1) && (kk==-1) && (len-jj >=2) && (len-jj<=3)) {
  17.      }
  18.      else {
  19.          alert("Please enter a valid email address.\n" +
  20.         document.frm.email.value + " is invalid.");
  21.         document.frm.email.focus() ;
  22.         return false; }
  23.  
  24.         if (document.frm.username.value.replace(/ /g, "") == "")  {
  25.             alert("Please enter desired username. Must be between 5-18 characters (no special characters).")
  26.             document.frm.username.focus() ;
  27.         return false; }
  28.  
  29.         var regExp = /^[A-Za-z][A-Za-z0-9_]*$/;
  30.    if(!regExp.test(document.frm.username.value))
  31.      alert("no special characters or spaces in 'username'")
  32.    if(document.frm.username.value.length > 18 || document.frm.username.value.length < 5) {
  33. alert("'Username' should be between 5-18 characters.")
  34.  document.frm.username.focus() ;
  35.             return false; }
  36.  
  37.         if (document.frm.password.value=="") {
  38.             alert("Please enter desired password; must be between 5 and 15 characters.")
  39.             document.frm.password.focus() ;
  40.             return false; }
  41.  
  42.                 if(document.frm.password.value.length < 5) {  
  43.    alert("Minimum 5 characters for 'password'.")
  44.   document.frm.password.focus() ;
  45.    return false; }
  46.  
  47.             if(document.frm.password.value.length > 15) {  
  48.    alert("Maximum 15 characters allowed for 'password'.")
  49.   document.frm.password.focus() ;
  50.    return false; }
  51.  
  52.  
  53.         if (document.frm.PASS2.value=="") {
  54.             alert("Please enter password confirmation.")
  55.             document.frm.PASS2.focus() ;
  56.         return false; }    
  57.  
  58.         if (document.frm.name.value=="") {
  59.             alert("Please enter your name in this format: Lastname, Firstname ")
  60.             document.frm.name.focus() ;
  61.         return false; }    
  62.  
  63.         if(document.frm.name.value.length < 5) {  
  64.    alert("Minimum 5 characters for 'name'.")
  65.   document.frm.name.focus() ;
  66.    return false; }
  67.  
  68.             if(document.frm.name.value.length > 30) {  
  69.    alert("Maximum 30 characters allowed for 'name'.")
  70.   document.frm.name.focus() ;
  71.    return false; }
  72.  
  73.  
  74.         if (document.frm.city.value.replace(/ /g, "") == "")  {
  75.             alert("Please enter your city.")
  76.             document.frm.city.focus() ;
  77.         return false; }
  78.  
  79.         if(document.frm.city.value.length < 3) {  
  80.    alert("Minimum 3 characters for 'city'.")
  81.   document.frm.city.focus() ;
  82.    return false; }
  83.  
  84.             if(document.frm.city.value.length > 20) {  
  85.    alert("Maximum 20 characters allowed for 'city'.")
  86.   document.frm.city.focus() ;
  87.    return false; }
  88.  
  89.         if (document.frm.state.value.replace(/ /g, "") == "")  {
  90.             alert("Please enter your state.")
  91.             document.frm.state.focus() ;
  92.         return false; }
  93.  
  94.         if(document.frm.state.value.length < 2) {  
  95.    alert("Minimum 2 characters for 'state'.")
  96.   document.frm.state.focus() ;
  97.    return false; }
  98.  
  99.             if(document.frm.state.value.length > 20) {  
  100.    alert("Maximum 20 characters allowed for 'state'.")
  101.   document.frm.state.focus() ;
  102.    return false; }        
  103.  
  104.    var mycheckbox = document.frm.opt15; 
  105. if (mycheckbox.checked == false) { 
  106.    alert("this checkbox is mandatory") 
  107.    return false; } 
  108.   return true;     }
  109. } //<--this one 
Apr 29 '07 #6
RKZA
8
Hi,
Just focusing on the script in bold i can identify a simple error,
you have have forgot to open your curly bracket, see below,
if(document.frm.username.value.length > 18 || document.frm.username.value.length < 5) { <<<<missing bracket
[HTML]var regExp = /^[A-Za-z][A-Za-z0-9_]*$/;
if(!regExp.test(document.frm.username.value))
alert("no special characters or spaces in 'username'")
if(document.frm.username.value.length > 18 || document.frm.username.value.length < 5) {//missing bracket
alert("'Username' should be between 5-18 characters.")
document.frm.username.focus() ;
return false; }[/HTML]

So this would return false all the time without this bracket.

R.K.
Apr 29 '07 #7
karen987
114 100+
OK thanks the bracket did the trick for that one,

now ive got this problem, with the checkbox, the browser returns a debug error, i don't get the validation script, below, instead the browser says

error: document.frm.opt15. is null or not an object

when i debug it points to the below code, i wonder what is wrong with it? the validation does go through, it's just that i don't get the alert, but on the server validation, it shows that it goes through


Expand|Select|Wrap|Line Numbers
  1. var mycheckbox = document.frm.opt15; 
  2. if(mycheckbox.checked == false) { 
  3.    alert("this checkbox is mandatory") 
  4.    return false; }
ive tried,
Apr 29 '07 #8
pbmods
5,821 Expert 4TB
[sidenote post]

Regarding password length enforcement, I have never (nor do I ever plan to) require my Users to conform their passwords to any specification (aside from length > 0). Thanks to password managers on every platform, it is now feasible for a User to have his password be rot13([the entire text of War and Peace]). And the database doesn't care b/c it'll just SHA-1 the whole thing anyway. So we only ever need to store 40 characters.

Meanwhile, since my password reset/recovery process is completely automatic, it doesn't bother me at all if my Users are forgetting their passwords. It just means that they might want to consider a more memorable password, or else they'll have to keep resetting their passwords.

The fact is, the User's own perceptions of the importance of the role that your site plays in his life, as well as his own experiences and prejudices, will determine how secure he makes his password. To force the User to do otherwise really undermines one of the fundamental concepts of the web: the User is supposed to be in control at all times.

And aside from all that, the only reason that you would ever NEED to enforce a password length would be if you're storing it in cleartext. And it is for this reason that I am suspicious of "maximum password length".

Here's your soapbox back. Thanks for your time.
Apr 29 '07 #9
karen987
114 100+
[sidenote post]

Regarding password length enforcement, I have never (nor do I ever plan to) require my Users to conform their passwords to any specification (aside from length > 0). Thanks to password managers on every platform, it is now feasible for a User to have his password be rot13([the entire text of War and Peace]). And the database doesn't care b/c it'll just SHA-1 the whole thing anyway. So we only ever need to store 40 characters.

Meanwhile, since my password reset/recovery process is completely automatic, it doesn't bother me at all if my Users are forgetting their passwords. It just means that they might want to consider a more memorable password, or else they'll have to keep resetting their passwords.

The fact is, the User's own perceptions of the importance of the role that your site plays in his life, as well as his own experiences and prejudices, will determine how secure he makes his password. To force the User to do otherwise really undermines one of the fundamental concepts of the web: the User is supposed to be in control at all times.

And aside from all that, the only reason that you would ever NEED to enforce a password length would be if you're storing it in cleartext. And it is for this reason that I am suspicious of "maximum password length".

Here's your soapbox back. Thanks for your time.
pbmods,

I read your message, and i'm not sure i understand the part above. What are you suspicious of?

I'm limiting the password, simply because the other fields are limited too. I didn't know there was a taboo against it as you're implying. Nor did i understand what you said about password recovery and rots or whatever.

For the record, i'm not a programmer or web developer, and know hardly anything about these things, I am not html or ASP literate, and i've gathered together an assortment of validation functions, which i'm trying to gel together. I don't understand the different statements and functions, etc.

I suggest you think twice, before you accuse someone of some imaginary crime next time.

Thank you for you tips though.

All i want is to create validations which are all equal, in length where possible. I was not aware that is a crime.
Apr 30 '07 #10
RKZA
8
Hi dude,
So if you want to check that you check box is ticked, you would write

[HTML]if(!mycheckbox.checked){ //what ever code }[/HTML]

R.K.
Apr 30 '07 #11
I think you are missing the braces in

if(!regExp.test(frm.username.value))
alert("no special characters")
if(frm.username.value.length > 18 || frm.username.value.length < 5)
alert("username should be between 5-18 characters")

try with braces.
Apr 30 '07 #12

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

Similar topics

9
by: varois83 | last post by:
Hi Newbie here. I have been working on creating a guestbook for my site as practice and am learning a lot. Do you guys validate your forms first on the client with javascript and then on the...
5
by: Alex M | last post by:
I'm trying to use the jakarta struts client side javascript validators and the validators are working, but they are not showing the popup messages. I have the message keys defined in the...
6
by: bonehead | last post by:
Greetings, I'm working on an e-mail form (btw many thanks to Philip Ronan for the very cool email address format tester function, best I've seen so far). I've been trying, with limited...
3
by: Gary Varga | last post by:
In the file WebUIValidation.js, when a postback that doesn't fail the validation has a javascript error saying summary is undefined in the ValidationSummaryOnSubmit function....
4
by: TJS | last post by:
how do I get "onSubmit" to work in .net ? <Form id="Form1" name="Form1" method="post" onSubmit="return validateStandard(this, 'error');" runat="server">
4
by: Ian Cox | last post by:
I have a web form that contains a Datagrid. This grid has a number of columns, one of which contains a text box and validator for that text box. Everything works fine, when I press the "Save"...
2
by: lbolognini | last post by:
Hi all, I'd like everything to be done server-side and don't like to see any Javascript on my page source. Would it be possible also to prevent the validator controls render a blank space &nbsp;...
8
by: chrisdude911 | last post by:
how do i add video into a javascript web page with my own custom buttons?
27
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it...
13
by: Mtek | last post by:
Hi, We have a form defined with buttons like this: <a class="save_menu" href="javascript:document.Detail_Screen.action = 'savedata.php?screen=EDIT';document.Detail_Screen.submit();">Update</...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.