Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8538707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:11:09+00:00 2026-06-11T11:11:09+00:00

Im new to php and trying to validate some fields in the user form

  • 0

Im new to php and trying to validate some fields in the user form using javascript.
The problme is sometimes it works and sometimes dont, and when the alert disappears all the fields that the user was alreay filled, is blank again.

Heres my php code :

<script>
            function validar(formulario){
                if(formulario.nome.value == ''){
                    alert("O campo NOME é obrigatório.");
                    return false;
                }
                if(formulario.Email.value == ''){
                    alert("O campo EMAIL é obrigatório.");
                    return false;
                }
                if(formulario.Email.value.indexOf(('@' && '.'),0)== -1){
                    alert("EMAIL invalido.");
                    return false;
                }
                if(formulario.usuario.value == ''){
                    alert("O campo USUÁRIO é obrigatório.");
                    return false;
                }
                if(formulario.senha.value == ''){
                    alert("O campo SENHA é obrigatório.");
                    return false;
                }
                if(formulario.uf.value.length > 3 || formulario.uf.length <=1){
                    alert("O campo UF é inválido.");
                    return false;
                }
                if(formulario.crm.value.length < 4){
                    alert("O número do CRM é inválido.");
                    return false;
                }
                if(formulario.cidade.value.value == ''){
                    alert("O número do CRM é inválido.");
                    return false;
                }
                return true;
            }
        </script>
</head>
<body>

<form id="formulario" method="post" onsubmit="return validar(this);" action="cadastrar.php">
 <B>Nome: </B><input type=text name=nome size="50"> <br>
<BR>
<B>E-mail: </B><input type=text name=Email size="50"><br>
<BR>
<B>Nome de Usuário: </B><input type=text name=usuario size="50"><br>
<BR>
<B>Senha: </B><input type=text name=senha size="10"><br>
<BR>
<B>CRM: </B><input type=text name=crm size="5"><br>
<BR>
<B>UF: </B><input type=text name=uf size="2"><br>
<BR>
<B>Cidade: </B><input type=text name=cidade size="20"><br>
<BR>
<B>Especialidade Médica: </B><input type=text name=esp size="30"><br>
<BR>

<input type="submit" name="Submit" value="Enviar" /> <br />

</form>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:11:10+00:00Added an answer on June 11, 2026 at 11:11 am

    Sloppy coding

    this will work better DEMO

    function validar(formulario){
      if(formulario.nome.value == ''){
        alert("O campo NOME é obrigatório.");
        return false;
      }
      if(formulario.Email.value == ''){
        alert("O campo EMAIL é obrigatório.");
        return false;
      }
      if(formulario.Email.value.indexOf(('@' && '.'),0)== -1){
        alert("EMAIL invalido.");
        return false;
      }
      if(formulario.usuario.value == ''){
        alert("O campo USUÁRIO é obrigatório.");
        return false;
      }
      if(formulario.senha.value == ''){
        alert("O campo SENHA é obrigatório.");
        return false;
      }
      if(formulario.crm.value.length < 4){
        alert("O número do CRM é inválido.");
        return false;
      }
      if(formulario.uf.value.length > 3 || formulario.uf.length <=1){
        alert("O campo UF é inválido.");
        return false;
      }
      if(formulario.cidade.value == ''){
        alert("O número do cidade é inválido.");
        return false;
      }
      return true;
    }​
    

    I also suggest adding

    function isEmail(str) { // http://stackoverflow.com/a/4964763/295783
      return /(.+)@(.+){2,}\.(.+){2,}/.test(str);
    }
    

    and have

    if(!isEmail(formulario.Email.value)){
      alert("EMAIL invalido.");
      return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use a PHP form (new.php) to update 2 MySQL tables
I am new to PHP and JavaScript development, I am trying to execute this
I am new to programming PHP and am trying to validate a field in
I am new to PHP and trying to learn some so sorry if this
I am new to php and trying to write a login function. Bit stuck
I'm new to PHP and trying to get my head around security. I have
I am new to PHP and trying to learn it by creating a database
I am new to PHP and was trying to build RestFull service in PHP
I'm new to php and I'm trying to create a simple example for calling
I am pretty new to PHP and I am trying to make an inventory

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.