Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Javascript - Validating required fields
Message
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Titre:
Javascript - Validating required fields
Divers
Thread ID:
01230279
Message ID:
01230279
Vues:
58
I hope it's ok to post this type of question in this forum. I wasn't sure where else to ask it.

I currently allow my customers to write html code that I save in a sql database. This html code is then rendered at the apppropriate time (based on specific urls). Anyway, in this code I allow my customers to name fields as they see fit and if they end the field name with "_req" I validate it (basically, just making sure there is a value).

This is pretty easy for text boxes or areas via the following javacript code:
function NewRentalForm_Validator(theForm)
{
  submitonce(theForm);
  var message = "Please do not leave the following fields Blank: ";
  var len = 0;
  var name = "";
  for (i=0;i<theForm.elements.length;i++)
  {
    name = theForm.elements[i].name;
    len = name.length;
    if (name.substring(0,4).toUpperCase() == "C2B_" && name.substring(len-4).toUpperCase() == "_REQ" && theForm.elements[i].value.length == 0 )
    {
       message = message + "\n   " + name.substring(4,len-4);
    }
  }
  if (message.length > 48)
  {
    alert(message);
    submitenabled(theForm);
    return (false);
  }
  return (true);
}
Now for radio buttons, I was thinking something along the lines of changing the if statement to be something like:
    if (name.substring(0,4).toUpperCase() == "C2B_" && name.substring(len-4).toUpperCase() == "_REQ" && (theForm.elements[i].value.length == 0 || !theForm.elements[i].checked))
Of course the preceeding code would make the following radio button cause the error message because one of the options wouldn't be selected
<INPUT type=radio value=male name=c2b_Sex_req> Male <BR>
<INPUT type=radio value=female name=c2b_Sex_req> Female 
If I was writing the name of the options etc, I know I could hardcode and make sure that one option of the group was checked. I'm having a problem because I won't know what the names of these option groups might be.

Any help or pointers are very much appreciated.

Paul
Répondre
Fil
Voir

Click here to load this message in the networking platform