1

I have one form-validation sample. If I press submit button, it is has to show alert message but it is not showing.

This is my sample: sample link

Please help me to get this done.

I have tried like this:

//button declaration

<button id="validateSubmit" [disabled]="reactForm.invalid" onclick="myFunction()" class="samplebtn e-control e-btn e-primary"
                            type="submit" style="height:40px;width: 150px;" data-ripple="true">Submit</button>
                            
//function for showing alert message
<script>
    function myFunction() {
        alert('your form is submitted');
    }
</script>

2 Answers 2

-1

Really important to respect the code convention. In computer programming, an indentation style is a convention governing the indentation of blocks of code to convey program structure. However, some languages (such as Python and occam) use indenting to determine the structure instead of using braces or keywords; this is termed the off-side rule.

If you make some multi-space like you did, it will create an error as it will consider your class attribute value would be samplebtn e-control e-btn e-primary" type="submit" style="height:40px;width: 150px;" data-ripple="true">.

Then after it, it thinks the Submit is another attribute of your button. That is why it created the error where it didn't know how to handle your < after.

So again, in order to avoid that error, please respect the convention as below and it will work great.

Same line convention

function myFunction() {
  alert('your form is submitted');
}
<button id="validateSubmit" [disabled]="reactForm.invalid" onclick="myFunction()" class="samplebtn e-control e-btn e-primary" type="submit" style="height:40px; width: 150px;" data-ripple="true">Submit</button>

Indentation convention

One other way to write it is to indented every attributes.

function myFunction() {
  alert('your form is submitted');
}
<button
  id="validateSubmit"
  [disabled]="reactForm.invalid"
  onclick="myFunction()"
  class="samplebtn e-control e-btn e-primary"
  type="submit"
  style="height:40px;width: 150px;"
  data-ripple="true">Submit</button>

Sign up to request clarification or add additional context in comments.

3 Comments

No doubt it's working, but you should at least tell what you changed to make it work.
can you please tell me what did you change?... stackblitz.com/edit/…
Hahaha np, anytime my friend!
-1

Ideally its best to use typescript itself to your action. if so move your function to typescript file itself rather than in script.Sample

If you want to use javascript from angular see here

You could find errors from the console window which helps to debug.

export class ReactiveFormReactiveComponent implements OnInit {

reactForm: FormGroup;

constructor() {
  this.reactForm = new FormGroup({
    'check': new FormControl('required', [FormValidators.required]),
    'email_check': new FormControl('[email protected]', [FormValidators.email]),
    'url_check': new FormControl('www.anything.com', [FormValidators.url]),

  });
}

ngOnInit(): void {

}

get check() { return this.reactForm.get('check'); }


  myFunction() {
     alert('your form is submitted');
 }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.