-
Notifications
You must be signed in to change notification settings - Fork 0
Exercise Dom by Raul Valencia #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,65 @@ | ||||||
| let form = document.querySelector('form'); | ||||||
|
|
||||||
| function structureIf(nameCondition){ | ||||||
| if (nameCondition.value == '') { | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| console.log(`${nameCondition.id}: It's need value`); | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. alert it too |
||||||
| nameCondition.classList.add('required'); | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can save the class name in a const.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would name it |
||||||
| return ""; | ||||||
| } else { | ||||||
| nameCondition.classList.remove("required"); | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||||||
| return nameCondition.value; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| function validEmail(){ | ||||||
| if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(Email.value)) { | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can save the RegEx in a const. It can be at the top of the code. |
||||||
| Email.classList.remove("required"); | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here you can use the const. |
||||||
| return Email.value; | ||||||
| } else { | ||||||
| Email.classList.add("required"); | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here you can use the const. |
||||||
| console.log(`${Email.id}: You have entered an invalid email address`) | ||||||
| alert("You have entered an invalid email address!"); | ||||||
|
Comment on lines
+20
to
+21
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you are mixing |
||||||
| return ""; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| function validNumber(){ | ||||||
| if (/\d+$/.test(Phone.value)) { | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can save the RegEx in a const. It can be at the top of the code. |
||||||
| Phone.classList.remove("required"); | ||||||
| return Phone.value; | ||||||
| } else { | ||||||
| Phone.classList.add("required"); | ||||||
| console.log(`${Phone.id}: You can only write numbers`) | ||||||
| alert("You can only write numbers!"); | ||||||
| return ""; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| function validCheckbox2(array){ | ||||||
| let check = ""; | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you are mixing |
||||||
| if (array[0].checked == false && array[1].checked == false && array[2].checked == false && array[3].checked == false) { | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can find an easier way to validate it
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Imagine that you have more checkboxes. Would you do it in the same way? |
||||||
| checkbox_id.classList.add("required"); | ||||||
| alert('At least one course must be selected'); | ||||||
| return ""; | ||||||
| } else { | ||||||
| checkbox_id.classList.remove("required"); | ||||||
| for (let index = 0; index < array.length; index++) { | ||||||
| if (array[index].checked == true) { | ||||||
| check += array[index].value+ ". "; | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you want to concat strings use template literals. I think that t's better to return the array with the checked values. |
||||||
| } | ||||||
| } | ||||||
| return check; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| function validaTeForm(event){ | ||||||
| event.preventDefault(); | ||||||
| let array = [form.course1, form.course2, form.course3, form.course4]; | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use a better name for your variables. |
||||||
| print_name.textContent = `Name: ${structureIf(Name)}`; | ||||||
| structureIf(Email); | ||||||
| print_email.textContent = `Email: ${validEmail()}`; | ||||||
| print_number.textContent = `Number: ${validNumber()}`; | ||||||
| print_checkbox.textContent = `Checkbox: ${validCheckbox2(array)}`; | ||||||
|
Comment on lines
+58
to
+62
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Print the form values in the HTML (below the form) if all of them are correct only
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, are
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same with |
||||||
| } | ||||||
|
|
||||||
| form.addEventListener('submit', validaTeForm); | ||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,64 @@ | ||||||
| <!DOCTYPE html> | ||||||
| <html lang="en"> | ||||||
| <head> | ||||||
| <meta charset="UTF-8"> | ||||||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||||||
| <link rel="stylesheet" href="style.css"> | ||||||
| <title>Forms</title> | ||||||
| </head> | ||||||
| <body> | ||||||
| <main> | ||||||
| <form> | ||||||
| <fieldset> | ||||||
| <legend><strong>Please enter your contact details</strong></legend> | ||||||
| <div> | ||||||
| <label for="Name"><strong>Your name (required):</strong></label> | ||||||
| <input type="text" id="Name" name="user_name" aria-label="required"> | ||||||
| <abbr title="required"></abbr> | ||||||
| </div> | ||||||
| <div> | ||||||
| <label for="Email"><strong>E-Mail address (required):</strong> </label> | ||||||
| <input id="Email" name="user_email" aria-label="required"> | ||||||
| <abbr title="required"></abbr> | ||||||
| </div> | ||||||
| <div> | ||||||
| <label for="Phone"><strong>Phone number (+57) (optional):</strong> </label> | ||||||
| <input id="Phone" name="user_number"> | ||||||
| </div> | ||||||
| </fieldset> | ||||||
| <fieldset id="checkbox_id"> | ||||||
| <legend><strong>Please select the courses that you are interested in (required)</strong></legend> | ||||||
| <div> | ||||||
| <input type="checkbox" id="webDeveloper" name="course1" value="Web Developer"> | ||||||
| <label for="webDeveloper"> <strong>Web Developer</strong></label> | ||||||
| </div> | ||||||
| <div> | ||||||
| <input type="checkbox" id="reactDeveloper" name="course2" value="React Developer"> | ||||||
| <label for="reactDeveloper"> <strong>React Developer</strong></label> | ||||||
| </div> | ||||||
| <div> | ||||||
| <input type="checkbox" id="fullStack" name="course3" value="Fullstack Developer"> | ||||||
| <label for="fullStack"> <strong>Fullstack Developer</strong></label> | ||||||
| </div> | ||||||
| <div> | ||||||
| <input type="checkbox" id="other" name="course4" value="Other"> | ||||||
| <label for="other"> <strong>Other</strong></label> | ||||||
| </div> | ||||||
| </fieldset> | ||||||
| <div> | ||||||
| <label for="textArea"><strong>Please enter a message (optional):</strong></label> | ||||||
| </div> | ||||||
| <div> | ||||||
| <textarea name="text" id="textArea" cols="50" rows="10"></textarea> | ||||||
| </div> | ||||||
| <button id="Button_submit">Send message</button> | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| <button type="reset">Reset form</button> | ||||||
| </form> | ||||||
| <p id="print_name">Name:</p> | ||||||
| <p id="print_email">Email:</p> | ||||||
| <p id="print_number">Number:</p> | ||||||
| <p id="print_checkbox">Checkbox:</p> | ||||||
|
Comment on lines
+57
to
+60
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not wrong. If you want to create it from js file you can use |
||||||
| <script src="form.js"></script> | ||||||
| </main> | ||||||
| </body> | ||||||
| </html> | ||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,3 @@ | ||||||
| .required{ | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| border: 1px solid red; | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use classes. e.g:
.contact-formto reuse code.