https://github.com/radhakishan404/validate-form-in-expo-style
This is a simple React-Native form InputBox with three main feature: 1- Floating Label, 2- Form Validation with custom error message, 3- Left - Right icon in InputBox
https://github.com/radhakishan404/validate-form-in-expo-style
Last synced: about 2 months ago
JSON representation
This is a simple React-Native form InputBox with three main feature: 1- Floating Label, 2- Form Validation with custom error message, 3- Left - Right icon in InputBox
- Host: GitHub
- URL: https://github.com/radhakishan404/validate-form-in-expo-style
- Owner: radhakishan404
- Created: 2020-11-05T17:39:15.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-12-12T11:31:28.000Z (over 2 years ago)
- Last Synced: 2025-03-12T21:11:53.279Z (2 months ago)
- Language: JavaScript
- Size: 1.42 MB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
***React-Native Expo Form Validation Component Library with Floating Label!***
===================
[](https://opensource.org/licenses/MIT)đ Hey! I'm Radhakishan Jangid đ. More about me [Here âŠ](https://radhakishan404.github.io/).
âšī¸ [**validate-form-in-expo-style**](https://github.com/radhakishan404/validate-form-in-expo-style) is a Simple form validation component with floating label for React-Native inspired by [react-native-form-validator](https://github.com/NewOldMax/react-native-validator-form). You can add floating label with this library and can validate form. I created this package for my personal use you can use it in yours too.
----------
My bad, that I don't have Mac or IPhone, so this library is tested only in android. Do check it in Mac and let me know if any problem occurs.
----------
***đ Table of Contents***
--
- [Install](#-install)
- [Supported Types](#%EF%B8%8F-supoorted-types-)
- [Default Validation Rules](#-default-validation-rules-are-)
- [Example](#-see-the-full-example-of-form-validation-in-react-native-expo-)
- [How to use](#-how-to-use-)
- [Props](#-props)
- [Methods](#-methods)
- [Contributing](#-contributing)
- [License](#-license)***đĨ Install***
--
```sh
$ npm install validate-form-in-expo-style
```
or
```sh
$ yarn add react-native-stylish-accordion
```
Now we need to install react-native-reanimated and react-lifecycles-compat.
If you are using Expo, to ensure that you get the compatible versions of the libraries, run:```
expo install react-native-reanimated react-native-gesture-handler react-lifecycles-compat
```If you are not using Expo, run the following:
```
yarn add react-native-reanimated react-native-gesture-handler react-lifecycles-compat
```***âī¸ Supported types:-***
----------
- TextInput
***đ Default Validation Rules are:-***
----------
- matchRegexp
- isEmail
- isEmpty
- required
- trim
- isNumber
- isFloat
- isPositive
- minNumber
- maxNumber
- minFloat
- maxFloat
- minStringLength
- maxStringLength
- isStringSome rules that are added in validationName can accept extra parameter for validation, like:
````javascript````
***đĨ See the full example of form validation in react-native Expo:-***
----------
[](https://youtu.be/y7bkVDu0LVA)***đĄ How to use:-***
----------````javascript
import React from 'react';
import { StyleSheet, View, Text, Dimensions, TouchableOpacity, Image, ScrollView } from 'react-native';
import { Form, InputText } from 'validate-form-in-expo-style';
import { FontAwesome, Feather } from "@expo/vector-icons";
class App extends React.Component {
state = {
first_name: "",
number: "",
last_name: "",
email: '',
user: { password: "", repeatPassword: "" },
}
componentDidMount() {
//You can add your own rules
Form.addValidationRule('isValidPassword', (value) => {
let passwordReg = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[a-zA-Z]).{8,}$/;
if (passwordReg.test(value) === false) {
return false;
}
return true;
});
Form.addValidationRule('isPasswordMatch', (value) => {
if (value !== this.state.user.password) {
return false;
}
return true;
});
}
componentWillUnmount() {
// Remove own rules
Form.removeValidationRule('isPasswordMatch');
Form.removeValidationRule('isValidPassword');
}handlePassword = (event) => {
const { user } = this.state;
user.password = event.nativeEvent.text;
this.setState({ user });
}handleRepeatPassword = (event) => {
const { user } = this.state;
user.repeatPassword = event.nativeEvent.text;
this.setState({ user });
}handleChange = (email) => {
this.setState({ email });
}handleFirstName = (first_name) => {
this.setState({ first_name });
}
handleLastName = (last_name) => {
this.setState({ last_name });
}
handleNumber = (number) => {
this.setState({ number });
}
submit = () => {
alert("form submit, thank you.")
}
handleSubmit = () => {
this.refs.form.submit();
}
render() {
let Image_Http_URL = { uri: 'https://radhakishan.vpran.in/img/radhakishan-web-3.jpg' };
const { user } = this.state;
return (
radhakishan404
validate-form-in-expo-style
}
invalidIcon={< Feather
name="alert-circle"
color="red"
size={20}
/>}
validIcon={}
labelStyle={styles.labelStyle}
style={[styles.inputStyle]}
containerStyle={styles.inputContainerStyle}
floatingTopValue={hp('1%')}
floatingFontSize={hp('0.5%')}
/>
}
invalidIcon={< Feather
name="alert-circle"
color="red"
size={20}
/>}
validIcon={}
labelStyle={styles.labelStyle}
style={[styles.inputStyle]}
containerStyle={styles.inputContainerStyle}
floatingTopValue={hp('1%')}
floatingFontSize={hp('0.5%')}
/>
}
invalidIcon={< Feather
name="alert-circle"
color="red"
size={20}
/>}
validIcon={}
labelStyle={styles.labelStyle}
style={[styles.inputStyle]}
containerStyle={styles.inputContainerStyle}
floatingTopValue={hp('1%')}
floatingFontSize={hp('0.5%')}
/>
}
invalidIcon={< Feather
name="alert-circle"
color="red"
size={20}
/>}
validIcon={}
labelStyle={styles.labelStyle}
style={[styles.inputStyle]}
containerStyle={styles.inputContainerStyle}
floatingTopValue={hp('1%')}
floatingFontSize={hp('0.5%')}
/>
}
passwordShowIcon={< Ionicons
name="eye-outline"
color={constants.white}
size={20}
/>}
validateNames={['isValidPassword', 'required']}
errorMessages={['Minimum eight characters, at least one uppercase letter, one lowercase letter and one number', 'This field is required']}
type="text"
value={user.password}
placeholder="custom password validation"
leftIcon={}
onChange={this.handlePassword}
labelStyle={styles.labelStyle}
style={[styles.inputStyle]}
containerStyle={styles.inputContainerStyle}
floatingTopValue={hp('1%')}
floatingFontSize={hp('0.5%')}
/>
}
leftIcon={}
labelStyle={styles.labelStyle}
style={[styles.inputStyle]}
containerStyle={styles.inputContainerStyle}
floatingTopValue={hp('1%')}
floatingFontSize={hp('0.5%')}
/>
Submit
);
}
}export default App;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
action: {
width: Dimensions.get('window').width,
padding: 20
},
appButtonContainer: {
elevation: 8,
backgroundColor: "#009688",
borderRadius: 10,
paddingVertical: 10,
paddingHorizontal: 12,
marginTop: 10
},
appButtonText: {
fontSize: 18,
color: "#fff",
fontWeight: "bold",
alignSelf: "center",
textTransform: "uppercase"
}
labelStyle: {
fontSize: hp('1.8%'),
color: constants.white,
paddingTop: hp('0.8%'),
opacity: .9,
// top: 20
},
inputStyle: {
color: constants.white,
paddingTop: hp('1%'),
},
inputContainerStyle: {
paddingBottom: hp('1%'),
paddingTop: hp('1.3%'),
borderWidth: 2,
borderBottomWidth: 2,
// borderColor: "#333333",
// borderBottomColor: "#333333",
borderColor: constants.primaryColor,
borderBottomColor: constants.primaryColor,
borderRadius: 15
},
inputIconStyle: {
marginHorizontal: 10,
fontSize: hp('2.3%'),
backgroundColor: "#333333",
borderRadius: 5,
alignSelf: "center",
paddingHorizontal: hp('0.2%'),
paddingVertical: hp('0.1%'),
}
});
````
***đ Props***
----
#### Form Props| Prop | Required | Type | Default value | Description |
|-----------------|----------|----------|---------------|------------------------------------------------------------------------------------------------------------------------------|
| onSubmit | true | function | | Callback for form that fires when all validations are passed |
| instantValidate | false | bool | true | If true, form will be validated after each field change. If false, form will be validated only after clicking submit button. |
| onError | false | function | | Callback for form that fires when some of validations are not passed. It will return array of elements which not valid. |
| debounceTime | false | number | 0 | Debounce time for validation i.e. your validation will run after `debounceTime` ms when you stop changing your input |#### InputText Props
| Prop | Required | Type | Default value | Description |
|-----------------|----------|----------|---------------|----------------------------------------------------------------------------------------|
| name | true | string | | Name of input field |
| label | false | string | | Name of input Floating Label |
| placeholder | false | string | | Placeholder of input before any value |
| validateNames | false | array | | Array of validation. See list of default validation rules in above example. |
| errorMessages | false | array | | Array of error messages. Order of messages should be the same as `validateNames` prop. |
| errorStyle | false | object | { container: { top: 0, left: 0, position: 'absolute' }, text: { color: 'red' }, underlineValidColor: 'gray', underlineInvalidColor: 'red' } } | Add your own error styles |
| validatorListener | false | function | | It triggers after each validation. It will return `true` or `false` |
| withRequiredValidator | false | bool | | Allow to use `required` validator in any validation trigger, not only form submit |
| leftIcon | false | code, image | | Either include image or add Icon tag code to display left icon see above example |
| invalidIcon | false | code, image | | Either include image or add Icon tag code to display error icon on right side see above example |
| validIcon | false | code, image | | Either include image or add Icon tag code to display success icon on right side see above example |
| secureTextEntry | false | bool | false | If true than show hide icon will get added automatically |***đ Methods***
----
#### Form Methods| Name | Params | Return | Descriptipon |
|------------------|--------|--------|----------------------------------------------------|
| resetValidations | | | Reset validation messages for all validated inputs |
| isFormValid | dryRun: bool (default true) | Promise | Get form validation state in a Promise (`true` if whole form is valid). Run with `dryRun = false` to show validation errors on form |
#### InputText Methods| Name | Params | Return | Descriptipon |
|------------------|--------|--------|----------------------------------------------------|
| getErrorMessage | | | Get error validation message |
| validate | value: any, includeRequired: bool | | Run validation for current component |
| isValid | | bool | Return current validation state |
| makeInvalid | | | Set invalid validation state |
| makeValid | | | Set valid validation state |***đŧ Contributing***
----This component covers all my needs, but feel free to contribute.
***đ License***
---
[MIT Š Radhakishan Jangid](https://radhakishan.vpran.in)