form validation react
JavaScript
class Test extends React.Component {
constructor(props){
super(props);
this.state = {
fields: {},
errors: {}
}
}
handleValidation(){
let fields = this.state.fields;
let errors = {};
let formIsValid = true;
//Name
if(!fields["name"]){
formIsValid = false;
errors["name"] = "Cannot be empty";
}
if(typeof fields["name"] !== "undefined"){
if(!fields["name"].match(/^[a-zA-Z]+$/)){
formIsValid = false;
errors["name"] = "Only letters";
}
}
//Email
if(!fields["email"]){
formIsValid = false;
errors["email"] = "Cannot be empty";
}
if(typeof fields["email"] !== "undefined"){
let lastAtPos = fields["email"].lastIndexOf('@');
let lastDotPos = fields["email"].lastIndexOf('.');
if (!(lastAtPos < lastDotPos && lastAtPos > 0 && fields["email"].indexOf('@@') == -1 && lastDotPos > 2 && (fields["email"].length - lastDotPos) > 2)) {
formIsValid = false;
errors["email"] = "Email is not valid";
}
}
this.setState({errors: errors});
return formIsValid;
}
contactSubmit(e){
e.preventDefault();
if(this.handleValidation()){
alert("Form submitted");
}else{
alert("Form has errors.")
}
}
handleChange(field, e){
let fields = this.state.fields;
fields[field] = e.target.value;
this.setState({fields});
}
render(){
return (
<div>
<form name="contactform" className="contactform" onSubmit= {this.contactSubmit.bind(this)}>
<div className="col-md-6">
<fieldset>
<input ref="name" type="text" size="30" placeholder="Name" onChange={this.handleChange.bind(this, "name")} value={this.state.fields["name"]}/>
<span style={{color: "red"}}>{this.state.errors["name"]}</span>
<br/>
<input refs="email" type="text" size="30" placeholder="Email" onChange={this.handleChange.bind(this, "email")} value={this.state.fields["email"]}/>
<span style={{color: "red"}}>{this.state.errors["email"]}</span>
<br/>
<input refs="phone" type="text" size="30" placeholder="Phone" onChange={this.handleChange.bind(this, "phone")} value={this.state.fields["phone"]}/>
<br/>
<input refs="address" type="text" size="30" placeholder="Address" onChange={this.handleChange.bind(this, "address")} value={this.state.fields["address"]}/>
<br/>
</fieldset>
</div>
</form>
</div>
)
}
}
React.render(<Test />, document.getElementById('container'));
Also in JavaScript:
- Title
- check to see if number is a decimal javascript
- Category
- JavaScript
- Title
- create dice game javascript
- Category
- JavaScript
- Title
- get url javascript
- Category
- JavaScript
- Title
- array shift javascript
- Category
- JavaScript
- Title
- how to make a screen recording software with js
- Category
- JavaScript
- Title
- add two numbers javascript
- Category
- JavaScript
- Title
- how to add multiple videos in html5 with javascript
- Category
- JavaScript
- Title
- how to turn decimales into percents with javascript
- Category
- JavaScript
- Title
- how to use style in react js
- Category
- JavaScript
- Title
- An unhandled exception occurred: Script file ../node_modules/jquery/dist/jquery.min.js does not exist.
- Category
- JavaScript
- Title
- how to import npm packages in node.js?
- Category
- JavaScript
- Title
- chart.js radar chart
- Category
- JavaScript
- Title
- get selected option value
- Category
- JavaScript
- Title
- bind jquery trough name
- Category
- JavaScript
- Title
- how to paste in input field react native
- Category
- JavaScript
- Title
- definicion de un componente en angular
- Category
- JavaScript
- Title
- ERROR in ./node_modules/tns-core-modules/ui/core/view/view.js Module not found: Error: Can't resolve '@nativescript/core/ui/core/view/view' in '/home/deepali/projects/supersquad-client/node_modules/tns-core-modules/ui/core/view'
- Category
- JavaScript
- Title
- how to get textedit on mac without download
- Category
- JavaScript
- Title
- javascript array pop
- Category
- JavaScript
- Title
- How to check if a member has a role discord.js
- Category
- JavaScript
- Title
- export component in
- Category
- JavaScript
- Title
- event target javascript
- Category
- JavaScript
- Title
- delete node between indexes node list js
- Category
- JavaScript
- Title
- how to get the last element of an array in javascript
- Category
- JavaScript
- Title
- angular run with proxy
- Category
- JavaScript
- Title
- javascript change web page title
- Category
- JavaScript
- Title
- difference between e.preventdefault and e.stoppropagation and return false
- Category
- JavaScript
- Title
- Date gethours js
- Category
- JavaScript
- Title
- get the size of the screen javascript
- Category
- JavaScript
- Title
- funciones invocan a funciones javascript
- Category
- JavaScript
- Title
- bind in javascript
- Category
- JavaScript
- Title
- how to send json data in fetch request body
- Category
- JavaScript
- Title
- angular routing url params
- Category
- JavaScript
- Title
- how to get element by title js
- Category
- JavaScript
- Title
- javascript array to comma separated string
- Category
- JavaScript
- Title
- document addeventlistener click
- Category
- JavaScript
- Title
- how to make fizzbuzz in javascript
- Category
- JavaScript
- Title
- columndefs in datatable not working while setting width jquery
- Category
- JavaScript
- Title
- bootstrap datepicker format dd-mm-yyyy
- Category
- JavaScript
- Title
- how to use fetch in node js
- Category
- JavaScript
- Title
- close exit app react native
- Category
- JavaScript
- Title
- angular npm angular material
- Category
- JavaScript
- Title
- add option to select jquery
- Category
- JavaScript
- Title
- create react app in existing folder
- Category
- JavaScript
- Title
- filter in react native
- Category
- JavaScript
- Title
- how to know if a number has a decimal number js
- Category
- JavaScript
- Title
- google maps react
- Category
- JavaScript
- Title
- javascript button onclick programmatically
- Category
- JavaScript
- Title
- appendchild javascript
- Category
- JavaScript
- Title
- how to version a react app azure pipelines
- Category
- JavaScript
- Title
- backbone js event listener
- Category
- JavaScript
- Title
- how to draw flower petals around circle javascript
- Category
- JavaScript
- Title
- get contents between tags javascript
- Category
- JavaScript
- Title
- javascript access php variable
- Category
- JavaScript
- Title
- fill array with values javascript
- Category
- JavaScript
- Title
- angular moment pipe timeAgo
- Category
- JavaScript
- Title
- how to get the next item in map() js
- Category
- JavaScript
- Title
- how to position View absolute react native
- Category
- JavaScript
- Title
- insertion sort js
- Category
- JavaScript
- Title
- get youtube video id from url javascript
- Category
- JavaScript
- Title
- get all the child of the same class javascript
- Category
- JavaScript
- Title
- Bots server count discord js
- Category
- JavaScript
- Title
- going through every attributes of an object javascript
- Category
- JavaScript
- Title
- check if a key exists in an object javascript
- Category
- JavaScript
- Title
- check if message mentions users discord js
- Category
- JavaScript
- Title
- binary to int javascript
- Category
- JavaScript
- Title
- .pop js
- Category
- JavaScript
- Title
- bash parse json
- Category
- JavaScript
- Title
- ellipsis javascript
- Category
- JavaScript
- Title
- in javascript how to split string
- Category
- JavaScript
- Title
- how to remove spaces from strings javascript
- Category
- JavaScript
- Title
- get location from brwoser react
- Category
- JavaScript
- Title
- box shadow javascript style change
- Category
- JavaScript
- Title
- greater than or equal to javascript
- Category
- JavaScript
- Title
- javascript check if elements of one array are in another
- Category
- JavaScript
- Title
- javascript change page title
- Category
- JavaScript
- Title
- D3.js
- Category
- JavaScript
- Title
- array definition js
- Category
- JavaScript
- Title
- day to week
- Category
- JavaScript
- Title
- function click anywhere javascript
- Category
- JavaScript
- Title
- javascript and
- Category
- JavaScript
- Title
- delete with unlinksync node
- Category
- JavaScript
- Title
- 'react-scripts' n’est pas reconnu en tant que commande interne ou externe, un programme exécutable ou un fichier de commandes.
- Category
- JavaScript
- Title
- how to link javascript to html
- Category
- JavaScript
- Title
- javascript clear all intervals
- Category
- JavaScript
- Title
- brute force search javascript
- Category
- JavaScript
- Title
- function and returns node js
- Category
- JavaScript
- Title
- change bloodhound remote dynamically
- Category
- JavaScript
- Title
- array of in javascript
- Category
- JavaScript
- Title
- javascrip reverse text
- Category
- JavaScript
- Title
- clone an object in javascript
- Category
- JavaScript
- Title
- get current url js
- Category
- JavaScript
- Title
- adding element to array javascript
- Category
- JavaScript
- Title
- callback in response node.js
- Category
- JavaScript
- Title
- Javascript compare two arrays
- Category
- JavaScript
- Title
- ascii to char in javascript
- Category
- JavaScript
- Title
- ionic (Emitted value instead of an instance of Error
- Category
- JavaScript
- Title
- how to program an array in js
- Category
- JavaScript
- Title
- how to deep copy object in javascript
- Category
- JavaScript
- Title
- Elements in iteration expect to have 'v-bind:key' directives
- Category
- JavaScript